Na'im R. Tyson
2010-Jul-29 05:29 UTC
[R] Displaying Counts of Unused Factors in Contingency Tables with table()
R-philes, I have a question about displaying counts of unused factors using the table() function. I have two vectors with character data in them: local.labels("ah", "ah", "ah~") local.preds("ah", "ah", "ah") If I use the table function as shown below, I get an error because the number of levels do not match up. v.cont.table <- table(local.labels, local.preds, dnn=c("observed", "predicted")); Is there any way to get zero counts into the contingency table, or would I have to use a flat table? Any help with sample code, or a pointer to a previous post, would be much appreciated. Regards, Na'im
Dennis Murphy
2010-Jul-29 08:53 UTC
[R] Displaying Counts of Unused Factors in Contingency Tables with table()
Hi, table() is behaving as documented with respect to your example. local.labels is a *character* vector with two distinct values and local.preds is a *character* variable with one distinct value. If you were expecting your table to divine that you wanted to include 'ah~' as a missing value in your local.preds object, it's not going to happen under your current setup.> local.labels <- c("ah", "ah", "ah~") > local.preds <- c("ah", "ah", "ah") > table(local.labels, local.preds)local.preds local.labels ah ah 2 ah~ 1> class(local.labels)[1] "character"> class(local.preds)[1] "character" Is this what you had in mind? labels <- factor(local.labels) preds <- factor(local.preds, levels = c('ah', 'ah~'))> table(labels, preds)preds labels ah ah~ ah 2 0 ah~ 1 0> labels[1] ah ah ah~ Levels: ah ah~> preds[1] ah ah ah Levels: ah ah~ There is a distinction in R between character objects and factor objects. More generally, many [generic] functions behave differently depending on the type of object(s) supplied as input. HTH, Dennis On Wed, Jul 28, 2010 at 10:29 PM, Na'im R. Tyson <ntyson@clovermail.net>wrote:> R-philes, > > I have a question about displaying counts of unused factors using the > table() function. I have two vectors with character data in them: > > local.labels("ah", "ah", "ah~") > local.preds("ah", "ah", "ah") > > If I use the table function as shown below, I get an error because the > number of levels do not match up. > > v.cont.table <- table(local.labels, local.preds, dnn=c("observed", > "predicted")); > > Is there any way to get zero counts into the contingency table, or would I > have to use a flat table? Any help with sample code, or a pointer to a > previous post, would be much appreciated. > > Regards, > > Na'im > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Maybe Matching Threads
- Exporting Contingency Tables with xtable
- Coercing Output from mget() into Proper Data Frame
- Computing Confidence Intervals for AUC in ROCR Package
- Converting a Vector into a 2-level Factor
- ROCR Issue: Averaging Across Multiple Classifier Runs in ROC Curve