Description Usage Arguments Note Author(s) Source Examples
Shift a variable by an amount, and pad with NA or your value of choice.
1 |
x |
Vector to be shifted |
pad |
Value to use for padding |
shift |
Quantity to shift (forward or backward) |
Can shift forward or backwards
Gene Leynes (originally dnlbrky http://stackoverflow.com/users/1344789/dnlbrky)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | shift(1:10, 3)
## [1] 4 5 6 7 8 9 10 NA NA NA
shift(1:10, -1)
## [1] 4 5 6 7 8 9 10 NA NA NA
as.data.table(faithful)[1:5,list(eruptLengthCurrent=eruptions,
eruptLengthTwoPrior=shift(eruptions,-2,0),
eruptLengthThreeFuture=shift(eruptions,3))]
# eruptLengthCurrent eruptLengthTwoPrior eruptLengthThreeFuture
# 1: 3.600 0.000 2.283
# 2: 1.800 0.000 4.533
# 3: 3.333 3.600 NA
# 4: 2.283 1.800 NA
# 5: 4.533 3.333 NA
shift(1:10, 3, NaN)
# [1] 4 5 6 7 8 9 10 NaN NaN NaN
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.