Home:ALL Converter>Function for lagged sums

Function for lagged sums

Ask Time:2015-12-03T00:26:18         Author:David S

Json Formatter

I know how to take the lagged difference:

delX = diff(x)

But the only way I know to take the lagged sum is:

sumY = apply(embed(c(0,y),2),1, sum)

Is there a function that can take the lagged sum? This way (or sliding the index in some other fashion) is not very intuitive.

Author:David S,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/34047819/function-for-lagged-sums
Matthew Plourde :

You're looking for filter:\n\nx <- 1:10\nfilter(x, filter=c(1,1), sides=1)\n# [1] NA 3 5 7 9 11 13 15 17 19\n\n\nYou could also use head and tail:\n\nhead(x, -1) + tail(x, -1)\n# [1] 3 5 7 9 11 13 15 17 19\n",
2015-12-02T16:36:00
yy