Skip to content

How to write efficient R code

1 message · Liaw, Andy

#
I'm guessing what Sebatian want is to do the differencing by a stratifying
variable such as ID; e.g., the data may look like:

df <- as.data.frame(cbind(ID=rep(1:5, each=3), x=matrix(rnorm(45), 15, 3))

So using Tom's solution, one would do something like:

mdiff <- function(x) x[-1,] - x[nrow(x),]
sapply(split(df[,-1], df[,1]), mdiff)

There could well be more efficient ways!

Andy
------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments,...{{dropped}}