[R] ave and grouping

Dieter Menne dieter.menne at menne-biomed.de
Mon Mar 2 14:01:04 CET 2009


Patrick Hausmann <Patrick.Hausmann <at> uni-bremen.de> writes:

> # I have a DF like this:
> sleep$b   <- c(rep(8,10), rep(9,10))
> sleep$me  <- with(sleep, ave(extra, group, FUN = mean))
> sleep
> 
> # I would like to create a new variable
> # holding the b-th value of group 1 and 2.
> 
> # This is not what I want, it takes always the '8' from group '1'
> # and not the '9'
> sleep$gr  <- with(sleep, ave(extra, group, FUN = function(x) x[ b[1] ]))
> sleep

Nice example, but I don't fully understand what you want.

sleep$b[1]
8

Or could it be that you got into the factor-trap? Try

R-FAQ How-do-I-convert-factors-to-numeric_003f

Dieter




More information about the R-help mailing list