[R] count() function

David Winsemius dwinsemius at comcast.net
Fri Apr 6 05:25:25 CEST 2012


On Apr 5, 2012, at 3:15 PM, Christopher R. Dolanc wrote:

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

Color me puzzled. The plyr package _has_ a count function.

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