[R] table() of a factor

Allan Engelhardt allane at cybaea.com
Tue Jun 29 13:06:10 CEST 2010


You could try

x<- factor(c(rep("a",3),"b","d"), levels=letters[1:4])
table(x)
# x
# a b c d
# 3 1 0 1


Hope this helps

Allan

On 29/06/10 11:59, Robin Hankin wrote:
> Hi
>
> suppose I have a factor 'x':
>
> > x <- as.factor(c(rep("a",3),"b","d"))
> > table(x)
> x
> a b d
> 3 1 1
> >
> >
>
> But this is not what I want because
> I need to include the fact that the count of "c" is zero.
>
> I can't just change the levels of x:
>
> > levels(x) <- c("a","b","c","d")
> > table(x)
> x
> a b c d
> 3 1 1 0
> >
>
> because this records the single "d" in the original 'x' as a "c".
>
>
> What I want is:
>
> a b c d
> 3 1 0 1
>
>
> How to get this from 'x'?
> (my real application has dozens of levels with complicated names).
>
>
>



More information about the R-help mailing list