shift | R Documentation |
This function computes lagged or leading values of a vector by a specified number of observations. By default, the function returns lag-1 values of the vector specified in the argument 'x'.
shift(x, n = 1, fill = NA, as.na = NULL, check = TRUE)
x |
a numeric vector. |
n |
a numeric value denoting the offset by which to lag (positive
value) or lead (negative value), e.g. |
fill |
a numeric value or |
as.na |
a numeric vector indicating user-defined missing values, i.e.
these values are converted to |
check |
logical: if |
Returns a numeric vector with the same length containing lagged or leading values.
This function is a modified copy of the lag()
and lead()
functions
in the dplyr package by Wickham et al. (2023).
Hadley Wickham, Romain Francois, Lionel Henry, and Kirill Müller, and Davis Vaughan.
Wickham H, Francois R, Henry L, Müller K, & Vaughan D (2023). dplyr: A grammar of data manipulation. R package version 1.1.3, <https://CRAN.R-project.org/package=dplyr>.
center
, rec
, dummy.c
, item.reverse
.
#--------------------------------------
# Lagged values
# Lag-1 values
shift(1:10)
# Lag-2 values
shift(1:10, n = 2)
# Value -99 to pad 'x'
shift(1:10, fill = -99)
#--------------------------------------
# Leading values
# Lead-1 values
shift(1:10, n = -1)
# Lead-2 values
shift(1:10, n = -2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.