I'm trying to write a code that get every 7th cumulative sum value from a vector, but it's running very slowly using a loop structure, because my data is so large.
Here is the code that i write. The results are the expected but, as i said, it is running very slowly:
for (i in 1:50000) {
B[i] <- cumsum(A[i:50000])[7]
}
Example: A = 4 1 2 2 6 2 6 6 1 10 5 8 6 9 1
The results must be:
B[1] = (4+1+2+2+6+2+6) = 23
B[2] = (1+2+2+6+2+6+6) = 25
and so...
Thank you.
Base R:
use
filter:use
convolveuse
embed:use
sapply:Other packages: