Skip to content
Prev 235279 / 398500 Next

Doing operations by grouping variable

That's if the variables are visible.  If they are only in the data frame it's not much more difficult

d <- data.frame(group = rep(1:5, each=5), 
                variable = rnorm(25))
with(d, tapply(variable, group, max))


(Tip: avoid using attach().)

Bill Venables.  

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Michael Bedward
Sent: Tuesday, 21 September 2010 11:15 AM
To: Seth W Bigelow; Rhelp
Subject: Re: [R] Doing operations by grouping variable

Not sure why you think tapply is "awkward". Your example would be...

group <- rep(1:5, each=5)
variable <- rnorm(25)
tapply(variable, group, max)

...which looks quite elegant to me :)

Meanwhile, the reason your expression doesn't work is that you are
asking mistakenly for elements 1:5 repeatedly from the variable col.
If you just type d$variable[ d$group ] and compare the values to your
variable vector this should be clear.

Michael
On 21 September 2010 10:59, Seth W Bigelow <sbigelow at fs.fed.us> wrote:
______________________________________________
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.