[R] Conditional operation on data frame, shift/roll of vector

jiangrm jiangrm at gmail.com
Sat Sep 26 17:46:43 CEST 2009


Conditionally, when Ind of a certain row is 1, want to get sum or delta of Val in that row and 1 row
above.

Val	Ind                 Val	Ind  Del
10	0                    10	0    NA
11	0                    11	0    NA
13	1      ------->      13	1    24 or 2
16	0                    16	0    NA

A simple way I guess is to get shifted vector of Val (say, c(NA, 10, 11, 13)), add to or minus from
Val, then and logically AND with Ind.

Which function provides the shift operation of the vector Val?

Also welcomed if any better way to do this. Thanks.




More information about the R-help mailing list