Re: [R] "by" question

2009-06-24 Thread hadley wickham
You might also want to look at the plyr package, http://had.co.nz/plyr. In particular, ddply + transform makes these tasks very easy. library(plyr) ddply(mtcars, "cyl", transform, pos = seq_along(cyl), mpg_avg = mean(mpg)) Hadley On Wed, Jun 24, 2009 at 11:48 AM, David Hugh-Jones wrote: > That

Re: [R] "by" question

2009-06-24 Thread David Hugh-Jones
That seems to work. I should add that to make "ave" work like "by" one can do: mydata$newvar <- ave(1:nrow(mydata), mydata$some_factor, FUN= function (x) { x <- ds[x,] # ... etc... }) Thanks! David [[alternative HTML version deleted]] __ R-h

Re: [R] "by" question

2009-06-24 Thread jim holtman
How about something like this: > x id data 1 1 0.7773207 2 3 0.9606180 3 2 0.4346595 4 3 0.7125147 5 2 0.344 6 2 0.3253522 7 2 0.7570871 8 3 0.2026923 9 3 0.7111212 10 2 0.1216919 > # compute running sum for each ID > x$run <- ave(x$data, x$id, FUN=cumsum) > x id

[R] "by" question

2009-06-24 Thread David Hugh-Jones
Hello all I have a big data frame and I regularly want to break it down into subsets, calculate some new data, and add it back to the data frame. At the moment my technique seems a bit ugly and embarrassing. Something like: result <- by(mydata, mydata$some_factor, function (x) { # do something