Hi, I have a data.frame of the following type:
F = data.frame(read.table(textConnection(" A B 1 1 4 2 1 3 3 1 1 4 1 4 5 1 2 6 1 2 7 1 2 8 2 1 9 2 1 10 2 1 11 2 1 12 3 2 13 3 4 14 3 1 15 3 1 16 3 1"),head=TRUE,stringsAsFactors=FALSE)) F A B 1 1 4 2 1 3 3 1 1 4 1 4 5 1 2 6 1 2 7 1 2 8 2 1 9 2 1 10 2 1 11 2 1 12 3 2 13 3 4 14 3 1 15 3 1 16 3 1 I want to generate a new column in which I calculate the (cum)sum of the last 3 B's for each group A, so that F$C becomes: A B C 1 1 4 0 2 1 3 4 3 1 1 7 4 1 4 8 5 1 2 8 6 1 2 7 7 1 2 8 8 2 1 0 9 2 1 1 10 2 1 2 11 2 1 3 12 3 2 0 13 3 4 2 14 3 1 6 15 3 1 7 16 3 1 6 I tried this: library(zoo) F$C = rollapply(as.zoo(F$B), 3, FUN = function(x) cumsum(x)-(x),na.pad=TRUE) Thanks! -- View this message in context: http://r.789695.n4.nabble.com/Moving-window-per-group-tp3346225p3346225.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@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.