[R] table command

Greg Snow Greg.Snow at imail.org
Mon Apr 26 23:01:15 CEST 2010


Is the fact that 0 is a possible and interesting value (even in being absent) a property of the table? Or a property of the variable?

I would argue that it is probably a property of the variable, and this is the better way to work with it in R (some older programs forced us to specify this at the analysis stage and it can take a bit of an effort to break that habit and do the more sensible approach).

I would convert your data to factors (or ordered factors) and specify the levels, then table just works on its own:

# untested code
> s2 <- as.data.frame( lapply(s, function(x) factor(x, levels=0:1) ) )
> table( s2[,c(1,3)] )

Hope this helps,

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Shubha Vishwanath Karanth
> Sent: Saturday, April 24, 2010 7:59 AM
> To: r-help at r-project.org; r-help at stat.math.ethz.ch
> Subject: [R] table command
> 
> Hi,
> 
> 
> 
> Let s be a dataframe.
> 
> 
> 
> > s
> 
>    A B C
> 
>    0 0 1
> 
>    1 0 1
> 
>    1 0 1
> 
>    0 0 1
> 
>    1 0 1
> 
>    0 1 1
> 
>    0 1 1
> 
>    0 1 1
> 
>    0 0 1
> 
> 
> 
> > tab1=table(s[,c(1,2)])
> 
> > tab1
> 
>    B
> 
> A   0 1
> 
>   0 3 3
> 
>   1 3 0
> 
> 
> 
> > tab2=table(s[,c(1,3)])
> 
> > tab2
> 
>    C
> 
> A   1
> 
>   0 6
> 
>   1 3
> 
> 
> 
> 
> 
> The problem is I need to access frequency corresponding to (0,0).
> tab1[1] will give me the correct value while tab2[1] will not give the
> frequency which I expected. So, is there a possibility in the table
> command to have the order of tab1 and tab2 being equal? (here 2*2). May
> be by filling in the appropriate value as 0 or NA?
> 
> 
> 
> Thanks,
> 
> Shubha
> 
> 
> 
> This e-mail may contain confidential and/or privileged
> i...{{dropped:13}}
> 
> ______________________________________________
> 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