If I want to calculate the previous n
mean years with a lag of the current year, how would I accomplish this? Is it as simple as a "right" rolling mean window? Or is it a "left" rolling mean window? I'm not sure which window to use here.
Sample Data
set.seed(1234)
dat <- data.frame(year = c(1990:2010),
x = rnorm(21))
dat$x_lag1 <- lag(dat$x, 1)
It may be easier to think in terms of offsets. If you want a window of 3 then
align = "right"
corresponds to using a window based on offsets of -2, -1, 0, i.e. point before prior, prior and current point. The current point is the rightmost end of the window. Note that rollapplyr
with an r
on the end is the same as specifying align = "right"
align = "center"
corresponds to using a window based on offsets of -1, 0, 1, i.e. prior point current point and next point. The current point is the center of the window. This is the default for align=
.align = "left"
corresponds to using a window based on offsets of 0, 1, 2 i.e. current point, next point and point after that. The current point is the leftmost point of the window.rollapply
allows one to use the align=
specification or offset notation. To use the latter for width
specify a list containing a single vector defining the offsets. (The actual specification of width is to specify a vector of widths, one for each element of the input or a list of offset vectors; however, in both cases they recycle so the usual case of specifying a single scalar width or a list containing a single offset vector are specifical cases.)
Below we use align=
to take the mean of a window of 3 ending in the current point and also use offsets as an alternative. We show both data frames and zoo objects.
We have omitted fill=NA
for the zoo objects since they automatically align anyways so it is typically unnecessary to use it.
library(zoo)
r1 <- transform(dat, roll = rollapplyr(x, 3, mean, fill = NA))
r2 <- transform(dat, roll = rollapply(x, list(seq(-2, 0)), mean, fill = NA))
all.equal(r1, r2)
## [1] TRUE
z <- read.zoo(dat, FUN = identity)
r3 <- rollapplyr(z, 3, mean)
r4 <- rollmeanr(z, 3)
r5 <- rollapply(z, list(seq(-2, 0)), mean) # z from above
all.equal(r3, r4, r5)
## [1] TRUE
If you want the 3 prior points, i.e. offsets -3, -2, -1, i.e. not the current point but the 3 points prior to that, then the following would work. Note that lag
in the last line requires a time series and should not be used with plain vectors.
# r6 is data frame
r6 <- transform(dat, roll = rollapply(x, list(-seq(3)), mean, fill = NA))
# r7, r8, r9 are zoo objects
r7 <- rollapply(z, list(-seq(3)), mean) # z from above
r8 <- stats::lag(rollapplyr(z, 3, mean), -1)
r9 <- stats::lag(rollmeanr(z, 3), -1)
all.equal(r7, r8, r9)
## [1] TRUE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With