Hello, > > I'm trying to get a piecewise sum of every n rows in a given column in > this data set. >
Is this it? # Result as a matrix, each column a piecewise cumsum mat <- sapply(which(sens2$rownumber %% 2 == 0), function(row) cumsum(c(sens2[row-1, 3], sens2[row, 3]))) # The same but c() makes it a vector vec <- c(sapply(which(sens2$rownumber %% 2 == 0), function(row) cumsum(c(sens2[row-1, "X"], sens2[row, "X"])))) # See the results data.frame(sens2, CumSum=vec) If this is what you want, return to your original 'sapply' instruction and see that you were passing entire rows to 'function(row)', not just row numbers. It was too complicated. (If it's not, sorry, but I missed the point.) Hope this helps, Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/only-0s-may-be-mixed-with-negative-subscripts-tp4384887p4385803.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.