Your problem simplifies to:
- Drop the first
n
elements in a vector - Pad
n
values ofNA
at the end
You can do this with a simple function:
shift <- function(x, n){
c(x[-(seq(n))], rep(NA, n))
}
example$z <- shift(example$z, 2)
The result:
example
x y z
1 1 1 3
2 2 2 4
3 3 3 5
4 4 4 6
5 5 5 7
6 6 6 8
7 7 7 NA
8 8 8 NA