If the aim is to calculate a rolling sum of 3 values such that there are implicitly 0s added to ensure that the output has 5 elements even though the input has 3 then try these:
1) rollapply Multiply x
and y
and insert 0's depending on whether right, center or left alignment is used and depending on whether partial=
is used. align="center"
is the default of rollapply
and align = "right"
is the default of rollapplyr
.
library(zoo)
rollapply(c(0, x*y, 0), 3, sum, partial = TRUE)
## [1] 2 5 9 7 4
rollapplyr(c(x*y, 0, 0), 3, sum, partial = TRUE)
## [1] 2 5 9 7 4
rollapplyr(c(0, 0, x*y), 3, sum, align = "left", partial = TRUE)
## [1] 2 5 9 7 4
rollapply(c(0, 0, x*y, 0, 0), 3, sum)
## [1] 2 5 9 7 4
rollsum(c(0, 0, x*y, 0, 0), 3) # this solution has the lowest character count
## [1] 2 5 9 7 4
2) Base R A base solution can be written using embed
:
rowSums(embed(c(0, 0, x*y, 0, 0), 3))
## [1] 2 5 9 7 4
2a) or take the cumulative sum and subtract the cumulative sum 3 back:
cumsum(c(x*y,0,0)) - cumsum(c(0, 0, 0, (x*y)[-3]))
## [1] 2 5 9 7 4
2b) If the idea is that a circular calculation is to be done then:
c(filter(c(0, x*y, 0), c(1,1,1), circular = TRUE))
## [1] 2 5 9 7 4