1\name{shift}
2\alias{shift}
3\alias{lead}
4\alias{lag}
5\title{Fast lead/lag for vectors and lists}
6\description{
7  \code{lead} or \code{lag} vectors, lists, data.frames or data.tables implemented in C for speed.
8
9  \code{bit64::integer64} is also supported.
10}
11
12\usage{
13shift(x, n=1L, fill=NA, type=c("lag", "lead", "shift"), give.names=FALSE)
14}
15\arguments{
16  \item{x}{ A vector, list, data.frame or data.table. }
17  \item{n}{ integer vector denoting the offset by which to lead or lag the input. To create multiple lead/lag vectors, provide multiple values to \code{n}; negative values of \code{n} will "flip" the value of \code{type}, i.e., \code{n=-1} and \code{type='lead'} is the same as \code{n=1} and \code{type='lag'}. }
18  \item{fill}{ Value to use for padding when the window goes beyond the input length. }
19  \item{type}{ default is \code{"lag"} (look "backwards"). The other possible values \code{"lead"} (look "forwards") and \code{"shift"} (behave same as \code{"lag"} except given names). }
20  \item{give.names}{default is \code{FALSE} which returns an unnamed list. When \code{TRUE}, names are automatically generated corresponding to \code{type} and \code{n}. If answer is an atomic vector, then the argument is ignored. }
21}
22\details{
23  \code{shift} accepts vectors, lists, data.frames or data.tables. It always returns a list except when the input is a \code{vector} and \code{length(n) == 1} in which case a \code{vector} is returned, for convenience. This is so that it can be used conveniently within data.table's syntax. For example, \code{DT[, (cols) := shift(.SD, 1L), by=id]} would lag every column of \code{.SD} by 1 for each group and \code{DT[, newcol := colA + shift(colB)]} would assign the sum of two \emph{vectors} to \code{newcol}.
24
25  Argument \code{n} allows multiple values. For example, \code{DT[, (cols) := shift(.SD, 1:2), by=id]} would lag every column of \code{.SD} by \code{1} and \code{2} for each group. If \code{.SD} contained four columns, the first two elements of the list would correspond to \code{lag=1} and \code{lag=2} for the first column of \code{.SD}, the next two for second column of \code{.SD} and so on. Please see examples for more.
26
27  \code{shift} is designed mainly for use in data.tables along with \code{:=} or \code{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 \code{give.names} to \code{TRUE}.
28}
29\value{
30  A list containing the lead/lag of input \code{x}.
31}
32
33\examples{
34# on vectors, returns a vector as long as length(n) == 1, #1127
35x = 1:5
36# lag with n=1 and pad with NA (returns vector)
37shift(x, n=1, fill=NA, type="lag")
38# lag with n=1 and 2, and pad with 0 (returns list)
39shift(x, n=1:2, fill=0, type="lag")
40# getting a window by using positive and negative n:
41shift(x, n = -1:1)
42shift(x, n = -1:1, type = "shift", give.names = TRUE)
43
44# on data.tables
45DT = data.table(year=2010:2014, v1=runif(5), v2=1:5, v3=letters[1:5])
46# lag columns 'v1,v2,v3' DT by 1 and fill with 0
47cols = c("v1","v2","v3")
48anscols = paste("lead", cols, sep="_")
49DT[, (anscols) := shift(.SD, 1, 0, "lead"), .SDcols=cols]
50
51# return a new data.table instead of updating
52# with names automatically set
53DT = data.table(year=2010:2014, v1=runif(5), v2=1:5, v3=letters[1:5])
54DT[, shift(.SD, 1:2, NA, "lead", TRUE), .SDcols=2:4]
55
56# lag/lead in the right order
57DT = data.table(year=2010:2014, v1=runif(5), v2=1:5, v3=letters[1:5])
58DT = DT[sample(nrow(DT))]
59# add lag=1 for columns 'v1,v2,v3' in increasing order of 'year'
60cols = c("v1","v2","v3")
61anscols = paste("lag", cols, sep="_")
62DT[order(year), (cols) := shift(.SD, 1, type="lag"), .SDcols=cols]
63DT[order(year)]
64
65# while grouping
66DT = data.table(year=rep(2010:2011, each=3), v1=1:6)
67DT[, c("lag1", "lag2") := shift(.SD, 1:2), by=year]
68
69# on lists
70ll = list(1:3, letters[4:1], runif(2))
71shift(ll, 1, type="lead")
72shift(ll, 1, type="lead", give.names=TRUE)
73shift(ll, 1:2, type="lead")
74
75# fill using first or last by group
76DT = data.table(x=1:6, g=rep(1:2, each=3))
77DT[ , shift(x, fill=x[1L]), by=g]
78DT[ , shift(x, fill=x[.N], type="lead"), by=g]
79
80}
81\seealso{
82  \code{\link{data.table}}
83}
84\keyword{ data }
85
86