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
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}}
On Thu, 2007-05-31 at 12:25 -0700, 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 ThompsonJonathan, Try this:> DFV1 V2 1 Plot1 big 2 Plot1 big 3 Plot1 small 4 Plot2 big 5 Plot2 small 6 Plot2 small 7 Plot3 small 8 Plot3 small 9 Plot3 small> with(DF, aggregate(V2, list(V1), function(x) names(which.max(table(x)))))Group.1 x 1 Plot1 big 2 Plot2 small 3 Plot3 small See ?which.max, ?names and ?table. HTH, Marc Schwartz
This should do the trick. Also labels ties with NA. a=as.data.frame(cbind(c(1,1,1,2,2,2,3,3,3,4,4),c ('big','big','small','big','small','small','small','small','small','big' ,'small'))) a$V2=factor(a$V2) maj=function(x){ y=table(x) z=which.max(y) if(sum(y==max(y))==1){ return(names(y)[z]) }else{ return(NA) } } aggregate(a$V2,list(a$V1),maj) On 31-May-07, at 4: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.-- Mike Lawrence Graduate Student, Department of Psychology, Dalhousie University Website: http://myweb.dal.ca/mc973993 Public calendar: http://icalx.com/public/informavore/Public "The road to wisdom? Well, it's plain and simple to express: Err and err and err again, but less and less and less." - Piet Hein
Martin Henry H. Stevens
2007-May-31 20:50 UTC
[R] Aggregate to find majority level of a factor
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"