[R] Doing operations by grouping variable

Michael Bedward michael.bedward at gmail.com
Tue Sep 21 03:15:01 CEST 2010


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:
> I'm writing an expression that requires searching a vector according to
> group. As an example, I want to find the maximum value in each of 5
> groups.
>
>
> group=rep(1:5, each=5)                          # create grouping variable
>
> variable=rnorm(25)                              # generate data
>
> d <- data.frame(group,variable)                         # make data frame
>
> max(d$variable[d$group])                        # try expression that
> doesn't work
>
> I'm expecting a vector containing the maximum variable value, per group.
> What am I doing wrong? I know I can use aggregate, tapply, etc. but that
> seems awkward and bulky, is there a simpler way?
>
>
> Dr. Seth  W. Bigelow
> Biologist, USDA-FS Pacific Southwest Research Station
> 1731 Research Park Drive, Davis California
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list