Reputation: 12087
I am trying to understand what the align
parameter does in rollapply
. I have read over the description in the documentation ?rollapply
(align):
specifyies whether the index of the result should be left- or right-aligned or centered (default) compared to the rolling window of observations. This argument is only used if width represents widths.
I am not clear what that means and there is no example. A similar question was asked here: alignment and offsets in rollapply but no one explained it.
Hoping someone can explain this in more detail with some examples. I tried experimentation to try to understand it better but I'm not sure how my results are being altered when I try align="right", "left" or "center".
Upvotes: 3
Views: 3941
Reputation: 270428
Right alignment. For the following it computes the result as y[i] = x[i-2] + x[i-1] + x[i]:
x <- 1:10
y <- rollapply(x, 3, sum, align = "right", fill = NA)
y
## [1] NA NA 6 9 12 15 18 21 24 27
The calculations are:
rollapply(x, 3, function(x) paste(x, collapse = "+"), align = "right", fill = NA)
## [1] NA NA "1+2+3" "2+3+4" "3+4+5" "4+5+6" "5+6+7"
## [8] "6+7+8" "7+8+9" "8+9+10"
An equivalent way of specifying this is in terms of offsets. That is, feed the element 2 positions back, 1 position back and the current position to sum
:
rollapply(x, list(c(-2, -1, 0)), sum, fill = NA) # same as align = "right"
Center alignment. For the following it computes the result as y[i] = x[i-1] + x[i] + x[i+1]:
x <- 1:10
y <- rollapply(x, 3, sum, align = "center", fill = NA)
y
## [1] NA 6 9 12 15 18 21 24 27 NA
The calculations are:
rollapply(x, 3, function(x) paste(x, collapse = "+"), align = "center", fill = NA)
## [1] NA "1+2+3" "2+3+4" "3+4+5" "4+5+6" "5+6+7" "6+7+8"
## [8] "7+8+9" "8+9+10" NA
An equivalent way to specify this is via offsets. That is feed the prior, current and next value to sum:
rollapply(x, list(c(-1, 0, 1)), sum, fill = NA) # same as align = "center"
Left alignment. For the following it computes the result as y[i] = x[i] + x[i+1] + x[i+2]:
x <- 1:10
y <- rollapply(x, 3, sum, align = "left", fill = NA)
y
## [1] 6 9 12 15 18 21 24 27 NA NA
The calculations are:
rollapply(x, 3, function(x) paste(x, collapse = "+"), align = "left", fill = NA)
## [1] "1+2+3" "2+3+4" "3+4+5" "4+5+6" "5+6+7" "6+7+8" "7+8+9"
## [8] "8+9+10" NA NA
This can also be specified in terms of offsets. That is use the current, next and position after next to feed to sum
:
rollapply(x, list(c(0, 1, 2)), sum, fill = NA) # same as align = "left"
Note that the right and center alignment can be written more compactly as:
y <- rollapplyr(x, 3, sum, fill = NA) # note r on the end to denote right
y <- rollapply(x, 3, sum, fill = NA) # no align specified so center
Upvotes: 5
Reputation: 132999
Here is an example:
library(zoo)
rollapply(1:10, c(1, 2, 3), sum, align = "right")
#1
#2+1
#3+2+1
#4
#5+4
#6+5+4
#7
#8+7
#9+8+7
#10
rollapply(1:10, c(1, 2, 3), sum, align = "center")
#1
#2+3
#2+3+4
#4
#5+6
#5+6+7
#7
#8+9
#8+9+10
#10
rollapply(1:10, c(1, 2, 3), sum, align = "left")
#1
#2+3
#3+4+5
#4
#5+6
#6+7+8
#7
#8+9
#omitted because there are not enough values, see partial
#10
Or simpler with a single number width:
rollapply(1:10, 3, sum, align = "right", partial = TRUE)
#[1] 1 3 6 9 12 15 18 21 24 27
rollapply(1:10, 3, sum, align = "center", partial = TRUE)
#[1] 3 6 9 12 15 18 21 24 27 19
rollapply(1:10, 3, sum, align = "left", partial = TRUE)
#[1] 6 9 12 15 18 21 24 27 19 10
Upvotes: 1