[R] Aggregate to find majority level of a factor

Peter Alspach PAlspach at hortresearch.co.nz
Thu May 31 22:22:47 CEST 2007


Jon

One way:  assuming your data.frame is 'jon'

aggregate(jon[,2], list(jon[,1]), function(x)
levels(x)[which.max(table(x))])
  Group.1     x
1   Plot1   big
2   Plot2 small
3   Plot3 small 

HTH ........

Peter Alspach

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of 
> Thompson, Jonathan
> Sent: Friday, 1 June 2007 7:26 a.m.
> To: r-help at stat.math.ethz.ch
> Subject: [R] Aggregate to find majority level of a factor
> 
> 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.
> 

______________________________________________________

The contents of this e-mail are privileged and/or confidenti...{{dropped}}



More information about the R-help mailing list