[R] Displaying Counts of Unused Factors in Contingency Tables with table()
Petr PIKAL
petr.pikal at precheza.cz
Thu Jul 29 16:17:52 CEST 2010
Hi
r-help-bounces at r-project.org napsal dne 29.07.2010 16:03:30:
> Dennis,
>
> Thank you for your response. For clarification, I was not expecting
> table() to divinely figure out the intent that I was making two levels
> out of one (given my character vectors). Since table() is a generic
> function that behaves differently with character and factor vectors, I
I am not sure if table behaves differently with character and factor
vectors.
x<-sample(letters[1:3], 10, replace=T)
table(x) # character
x
a b c
3 6 1
x.f<-as.factor(x) # same factor
table(x.f)
x.f
a b c
3 6 1
# factor with extra levels
x.f.extra.levels<-factor(x, levels=letters[1:6])
table(x.f.extra.levels)
x.f.extra.levels
a b c d e f
3 6 1 0 0 0
So if you do not supply extra levels to factor, result is same as with
character vector.
Regards
Petr
> needed a way to convert the data I had to get the desired behavior.
> Your factor() statements will accomplish my goal of putting zeros into
> contingency tables.
>
> Regards,
>
> Na'im
>
>
> Quoting "Dennis Murphy" <djmuser at gmail.com>:
>
> > 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 at 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 at 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.
> >>
> >
>
> ______________________________________________
> R-help at 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.
More information about the R-help
mailing list