[R] count() function

Christopher R. Dolanc crdolanc at ucdavis.edu
Thu Apr 5 21:15:48 CEST 2012


I keep expecting R to have something analogous to the =count function in 
Excel, but I can't find anything. I simply want to count the data for a 
given category.

I've been using the ddply() function in the plyr package to summarize 
means and st dev of my data, with this code:

ddply(NZ_Conifers,.(ElevCat, DataSource, SizeClass), summarise, 
avgDensity=mean(Density), sdDensity=sd(Density), n=sum(Density))

and that gives me results that look like this:

    ElevCat DataSource SizeClass avgDensity   sdDensity            n
1    Elev1        FIA    Class1   38.67768  46.6673478    734.87598
2    Elev1        FIA    Class2   27.34096  23.3232470    820.22879
3    Elev1        FIA    Class3   15.38758   0.7088432     76.93790
4    Elev1        VTM    Class1   66.37897  70.2050817  24958.49284
5    Elev1        VTM    Class2   39.40786  34.9343269  11782.95152
6    Elev1        VTM    Class3   21.17839  12.3487600   1461.30895

But, instead of "sum(Density)", I'd really like counts of "Density", so 
that I know the sample size of each group. Any suggestions?

-- 
Christopher R. Dolanc
Post-doctoral Researcher
University of California, Davis



More information about the R-help mailing list