[R] only 0s may be mixed with negative subscripts

Rui Barradas rui1174 at sapo.pt
Tue Feb 14 01:13:10 CET 2012


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.



More information about the R-help mailing list