Function
R ave() more convenient than its name suggests - it is basically the tapply() version, which allows you to return a vector of the same length as the input, and discards these values ββback in the same order as the input for you.
> x <- 1:10 > ave(x, x %% 2, FUN=function(d) d-mean(d)) [1] -4 -4 -2 -2 0 0 2 2 4 4
You can achieve a similar effect using ddply() , but this requires several additional copies of the data, as well as a couple of auxiliary variables:
> x <- 1:10 > val <- ddply(data.frame(x=x, id=1:10), .(x %% 2), function(d) {d$y <- d$x-mean(d$x); d}) > val[order(val$id),]$y [1] -4 -4 -2 -2 0 0 2 2 4 4
Is there any other plyr method that matches the lightweight approach I can get with ave() ?
source share