Home > front end >  How to Get Rolling Sum Without Returning NA's
How to Get Rolling Sum Without Returning NA's

Time:06-09

I am trying to get the rolling sum of the past N days without including NA's. Using this sample df:

myVec <- data.frame(myVec = c(7,2,4,5,1,3,2,9))

I'm aware of the function rollsumr using this approach:

library(zoo)
rollsumr(myVec$myVec, k = 3, fill = NA)

Which yields the following output: NA NA 13 11 10 9 6 14 However what I don't want is the leading NA's to take up those spots. I'd like to have the first spot be the first index, and second spot be 1st 2nd, and so on until the Nth spot is reached which looks like 7 9 13 11 10 9 6 14 as a final result.

I have a way to iterate through using a for loop if my N days was small, however if my rolling sum N were to be 50 with 100 rows, I'd have to index individually for each of the first 50 days. So, I'd think there is a simpler, more efficient way to accomplish this.

CodePudding user response:

You can use partial=TRUE in rollapplyr:

library(zoo)

rollapplyr(myVec$myVec, 3, sum, partial = TRUE)

# [1]  7  9 13 11 10  9  6 14

Or using the same function in dplyr:

library(dplyr)

myVec %>%
  mutate(myVec = rollapplyr(myVec, 3, sum, partial = TRUE))

CodePudding user response:

slider::slide_dbl is what you're looking for.

slider::slide_dbl(myVec$myVec, sum,.before = 2, .after = 0)
[1]  7  9 13 11 10  9  6 14
  • Related