You can use headand tailto create such a function:
shifter <- function(x, n = 1) {
if (n == 0) x else c(tail(x, -n), head(x, n))
}
Using:
a <- 1:4
shifter(a)
# [1] 2 3 4 1
shifter(a, 2)
# [1] 3 4 1 2
(Or, library(SOfun); shifter(a)where you can get SOfunfrom here ).
source
share