[R] summary per group

Sarah Goslee sarah.goslee at gmail.com
Mon Jan 2 14:34:06 CET 2012


Hi,

On Mon, Jan 2, 2012 at 8:08 AM, Johannes Radinger <JRadinger at gmx.at> wrote:
> 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?

nlevels() gives the number of levels defined, not the number of levels that have
members, so for your example will always be 5.
> with(df, aggregate(SPEC, by=list(FAM), nlevels))
  Group.1 x
1       A 5
2       B 5
3       C 5

You could drop the unused levels before checking:
> with(df, aggregate(SPEC, by=list(FAM), function(x)nlevels(factor(x))))
  Group.1 x
1       A 2
2       B 1
3       C 2

Or actually get a list of which ones are used:
> with(df, table(FAM, SPEC))
   SPEC
FAM a b c d e
  A 2 2 0 0 0
  B 0 0 3 0 0
  C 0 0 0 1 4

Sarah

-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list