Re: [R] summary of categorical variables

From: Chuck Cleland <ccleland_at_optonline.net>
Date: Mon, 21 Jan 2008 08:39:23 -0500

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

______________________________________________
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.
Received on Mon 21 Jan 2008 - 13:47:39 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 21 Jan 2008 - 14:00:08 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive