shift

functions
要素をずらす
Author

榎本剛

Published

October 31, 2024

Rにない函数。headtailを使う。

shift <- function(x, n) {
  if (n == 0) {
    x
  } else {
    c(tail(x, n), head(x, -n))
  }
}
shift(1:9,3)
[1] 7 8 9 1 2 3 4 5 6
shift(1:9,-2)
[1] 3 4 5 6 7 8 9 1 2