[R] table() of a factor

2010-06-29 Thread Robin Hankin
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

Re: [R] table() of a factor

2010-06-29 Thread Gavin Simpson
On Tue, 2010-06-29 at 11:59 +0100, 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:

Re: [R] table() of a factor

2010-06-29 Thread Allan Engelhardt
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

Re: [R] table() of a factor

2010-06-29 Thread Felix Andrews
Just use factor(), not levels(); you can pass a factor to factor() too. x - factor(c(rep(a,3),b,d), levels = letters[1:5]) table(x) x a b c d e 3 1 0 1 0 Cheers, -Felix On 29 June 2010 20:59, Robin Hankin rk...@cam.ac.uk wrote: Hi suppose I have a factor 'x': x -

Re: [R] table() of a factor

2010-06-29 Thread Robin Hankin
thanks everyone. I think the motto should be always specify the levels of a factor when you create it if you possibly can. best wishes Robin On 06/29/2010 12:39 PM, Felix Andrews wrote: Just use factor(), not levels(); you can pass a factor to factor() too. x-