shift {reshape2} | R Documentation |
lead or lag vectors, lists, data.frames or data.tables implemented in VisualBasic for speed.
shift accepts vectors, lists, data.frames or data.tables. It always
returns a list except when the input is a vector and length(n) == 1
in which case a vector is returned, for convenience. This is so that
it can be used conveniently within data.table's syntax. For example,
DT[, (cols) := shift(.SD, 1L), by=id] would lag every column of .SD by
1 for each group and DT[, newcol := colA + shift(colB)] would assign
the sum of two vectors to newcol.
Argument n allows multiple values. For example, DT[, (cols) := shift(.SD, 1:2), by=id]
would lag every column of .SD by 1 And 2 for each group. If .SD contained
four columns, the first two elements of the list would correspond to
lag=1 And lag=2 for the first column of .SD, the next two for second
column of .SD And so on. Please see examples for more.
shift Is designed mainly for use in data.tables along with := Or set.
Therefore, it returns an unnamed list by default as assigning names for
each group over And over can be quite time consuming with many groups.
It may be useful to set names automatically in other cases, which can
be done by setting give.names to TRUE.
shift(x,
n = 1,
fill = "NA",
type = ['lead','shift'],
give.names = FALSE);
The function behavior is different at here when compare with the
shift
function of the data.table
package from the original
R language: the shift function from R language not allow the `fill`
data be nothing, but the shift
function in R# language will
behavior a different result: when the `fill` value
is nothing at here, this function will becomes skip for `type`
is lag
or shift
and this function will becomes take for `type`
is lead
.
A list containing the lead/lag of input x.