[R] summary of categorical variables
Chuck Cleland
ccleland at optonline.net
Mon Jan 21 14:39:23 CET 2008
On 1/21/2008 8:08 AM, darteta001 at ikasle.ehu.es wrote:
> Dear list,
>
> I have a data.frame with nine categorical variables (0,1,2 and NAs)
> that I would like to get the number of events for each of them. I can
> extract this using summary() for each variable at a time with the
> as.factor()argument (otherwise it will get me the mean value):
>
>> summary(as.factor(mydf[,3]))
> 0 1 2 NA's
> 194 67 4 2
>
> Trying to use apply() to get this for all the columns of the
> data.frame I cannot use as.factor because it works with vectors and
> not data.frames.
How did you try to use apply()? Try something like this:
df <- as.data.frame(matrix(sample(c(0,1,2,NA), 9*267, replace=TRUE),
ncol=9))
apply(df, 2, function(x){summary(as.factor(x))})
V1 V2 V3 V4 V5 V6 V7 V8 V9
0 70 69 75 72 71 75 66 58 66
1 70 67 64 64 64 55 62 65 72
2 63 60 56 79 57 73 75 69 67
NA's 64 71 72 52 75 64 64 75 62
> Can anyone help me? Maybe a for loop would be good in this case
> instead, although I learnt that for loops are not recommended?
>
> Thanks in advance,
>
> David
>
> ______________________________________________
> 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.
--
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894
More information about the R-help
mailing list