[R] Aggregate to find majority level of a factor

Martin Henry H. Stevens HStevens at MUOhio.edu
Thu May 31 22:50:53 CEST 2007


How about tapply?

plot <- gl(2,3); plot
type <- letters[c(1,2,2,1,1,1)]; type
tapply(type, list(plot), function(x) {tabl <- table(x)
                                                 names(tabl[tabl==max 
(tabl)])})

Hank

On May 31, 2007, at 3:25 PM, Thompson, Jonathan wrote:

> I want to use the aggregate function to summarize data by a factor (my
> field plots), but I want the summary to be the majority level of  
> another
> factor.
>
>
> For example, given the dataframe:
>
> Plot1     big
> Plot1     big
> Plot1     small
> Plot2     big
> Plot2     small
> Plot2     small
> Plot3     small
> Plot3     small
> Plot3     small
>
>
> My desired result would be:
> Plot1 big
> Plot2 small
> Plot3 small
>
>
> I can't seem to find a scalar function that will give me the majority
> level.
>
> Thanks in advance,
>
> Jonathan Thompson
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.



Dr. Hank Stevens, Assistant Professor
338 Pearson Hall
Botany Department
Miami University
Oxford, OH 45056

Office: (513) 529-4206
Lab: (513) 529-4262
FAX: (513) 529-4243
http://www.cas.muohio.edu/~stevenmh/
http://www.muohio.edu/ecology/
http://www.muohio.edu/botany/

"E Pluribus Unum"



More information about the R-help mailing list