Reputation: 1
I have a dataset named "Dhaka" that looks like below:
I want to do the consecutive summation of the precipitation in a way so that it sums the precipitation of the three days (precipitation of two days before the exact date+ exact date precipitation).
The output may look like below:
the first value will be the same, second value will be the sum of the first and second value and the rest will be the summation of three precipitation value. I used
rollapply(precipitation, 3, sum)
but it did not give value date wise.
Upvotes: 0
Views: 49
Reputation: 1311
What you're looking for is cumsum:
library(data.table)
Dhaka <- data.table(Dhaka)[order(time)]
totalSum <- Dhaka[, cumsum(precipitation)]
Take the difference:
diffSum <- diff(totalSum, lag = 3)
finalSum <- c(totalSum[1:3], diffSum)
Dhaka[, sums := finalSum]
Upvotes: 1