[R] Summarizing counts by multiple factors
David Winsemius
dwinsemius at comcast.net
Wed May 12 03:56:29 CEST 2010
On May 11, 2010, at 9:08 PM, David Winsemius wrote:
>
> On May 11, 2010, at 8:38 PM, Noah Silverman wrote:
>
>> Hi,
>>
>> An example data set is:
>>
>> group level color
>> A 1 "blue"
>> A 1 "Red"
>> B 1 "blue"
>> B 2 "Red"
>> A 2 "Red"
>> B 2 "Red"
>> B 2 "blue"
>> B 2 "blue"
>> A 2 "blue"
>> A 2 "Red"
>>
>>
>> I'd like to compute a summary of counts for each combination of
>> group, level, color.
>>
>> An example output would be something like this:
>> group level color count
>> A 1 "blue" 1
>> A 2 "red" 2
>> B 2 "red" 2
>> etc..
>>
>>
>> The tapply function seems to do this for a single variable, but I
>> can figure out to to do it for combinations of more than one factor.
>
> This doesn't work?
>
> tapply(example$color, c(example$group, example$level), length)
I guess not. so perhaps:
as.data.frame( table(example) )
--
David
>
>
>> I also see the ave function, but can't figure out how to apply it
>> to multiple factors.
>
> It would give you as many lines as in the original which is not what
> you want.
>
> --
> David,
>>
>> Any suggestions?
>>
>> Thanks
>>
>> ______________________________________________
>> 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.
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> 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.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list