[R] - counting factor occurrences within a group: tapply()

jim holtman jholtman at gmail.com
Wed Jul 29 20:24:32 CEST 2009


Or even easier:

tapply(Trees$SppID, Trees$PlotID, function(x) length(unique(na.omit(x))))


On Wed, Jul 29, 2009 at 2:13 PM, Ian Chidister<ian.chidister at gmail.com> wrote:
> Hi All-
>
> Thanks for your quick responses.  I was looking for unique instances, so
> Jim's and Daniel's suggestions got the job done.  Using "length" alone
> didn't discriminate between multiple occurrences of the same species and
> multiple species.
>
> I do have one followup question- my full data set (not the example data) has
> a number of NAs in the SppID column, and [r] is currently counting the NAs
> as species occurrences.  Using Jim's code, I tried:
>
>>tapply(SppID, PlotID, function(Trees, na.rm=T) length(unique(Trees,
> na.rm=T)))
>
> and alternately:
>
>>tapply(SppID, PlotID, function(Trees) length(unique(Trees)), na.rm=T)
>
> which doesn't seem to convince [r] to ignore the NAs.  What am I doing
> wrong?
>
> Thanks,
>
> Ian
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list