[R] summary per group

David Winsemius dwinsemius at comcast.net
Mon Jan 2 18:31:59 CET 2012


On Jan 2, 2012, at 11:14 AM, Johannes Radinger wrote:

>
>
> Thank you Petr,
>
> that is exactly what I was looking for... no I played a little bit  
> around with that because I want to create a summary with FAM as a  
> grouping variable. Beside the number of unique SPEC per FAM also  
> want to get their levels as text. So far I know I have following:
>
> paste(unique(SPEC), collapse = ', ')
>
> But how can I use that in combination with tapply and furthermore  
> with cbind like:
>
> SPEC <- factor(c("a","a","b","b","c","c","c","d","e","e","e","e"))
> FAM <- factor(c("A","A","A","A","B","B","B","C","C","C","C","C"))
> df <- data.frame(SPEC,FAM)
>
> with(df, cbind("Number of  
> SPEC"=sapply(tapply(SPEC,FAM,unique),length),  
> "SPECs"=tapply(SPEC,FAM,unique)))

You probably do not want yoru resutls coming back as tables (which is  
what tapply returns, but as vectors.) So use `ave` instead of `tapply`.
>
> The result should look like:
>    Number of SPEC SPECs
> A   2              "a, b"
> B   1              "c"
> C   2              "d, e"
>
> Thank you,
>
> /johannes
>

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list