[R] summary per group

Johannes Radinger JRadinger at gmx.at
Mon Jan 2 17:14:21 CET 2012



-------- Original-Nachricht --------
> Datum: Mon, 2 Jan 2012 14:29:07 +0100
> Von: Petr PIKAL <petr.pikal at precheza.cz>
> An: "Johannes Radinger" <JRadinger at gmx.at>
> CC: r-help at r-project.org
> Betreff: Re: [R] summary per group

> Hi
> 
> > 
> > Hello,
> > 
> > I know that it'll be quite easy to do what I want but somehow I am lost 
> as
> > I am new to R. I want to get summary results arranged by groups. In 
> detail
> > I'd like get the number (levels) of Species per Family like for this 
> dataset:
> > 
> > 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)
> > 
> > I tried tapply(SPEC, FAM, nlevels).. but it is not the result I am 
> looking for...
> > 
> > What is the easiest way to do that? Do I have to rearrange the dataset?
> 
> To do what? Do you want number of unique entries within each level of FAM?
> If yes
> 
> sapply(tapply(SPEC, FAM, unique), length)
> 
> can do this.
> 
> Regards
> Petr

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)))

The result should look like:
    Number of SPEC SPECs
A   2              "a, b"
B   1              "c"
C   2              "d, e"

Thank you,

/johannes


--



More information about the R-help mailing list