Shift the elements of a vector in circular mode by k elements to the right (for positive k) or to the left (for negative k), such that the first element is at the (k+1)th position of the new vector and the last k elements are appended to the beginning.
VecShift does not attach the superfluous elements on one side to the other, but fills the resulting gaps with NAs.

VecRot(x, k = 1)
VecShift(x, k = 1)

Arguments

x

a vector of any type.

k

the number of elements to shift.

Details

The function will repeat the vector two times and select the appropriate number of elements from the required shift on.

Value

the shifted vector in the same dimensions as x.

Author

Andri Signorell <andri@signorell.net>

See also

[, rep, lag

Examples

VecRot(c(1,1,0,0,3,4,8), 3)
#> [1] 3 4 8 1 1 0 0

VecRot(letters[1:10], 3)
#>  [1] "h" "i" "j" "a" "b" "c" "d" "e" "f" "g"
VecRot(letters[1:10], -3)
#>  [1] "d" "e" "f" "g" "h" "i" "j" "a" "b" "c"

VecShift(letters[1:10], 3)
#>  [1] NA  NA  NA  "a" "b" "c" "d" "e" "f" "g"
VecShift(letters[1:10], -3)
#>  [1] "c" "d" "e" "f" "g" "h" "i" "j" NA  NA  NA