Re: [R] Aggregate to find majority level of a factor
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@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 __ R-help@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.
Re: [R] Aggregate to find majority level of a factor
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 Thompson Jonathan, Try this: > DF V1V2 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 __ R-help@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.
Re: [R] Aggregate to find majority level of a factor
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: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of > Thompson, Jonathan > Sent: Friday, 1 June 2007 7:26 a.m. > To: r-help@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@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}} __ R-help@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.
Re: [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@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" __ R-help@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.
[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@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.