EVOLUTION-MANAGER
Edit File: shift.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Fast lead/lag for vectors and lists</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for shift {data.table}"><tr><td>shift {data.table}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Fast lead/lag for vectors and lists</h2> <h3>Description</h3> <p><code>lead</code> or <code>lag</code> vectors, lists, data.frames or data.tables implemented in C for speed. </p> <p><code>bit64::integer64</code> is also supported. </p> <h3>Usage</h3> <pre> shift(x, n=1L, fill=NA, type=c("lag", "lead", "shift"), give.names=FALSE) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p> A vector, list, data.frame or data.table. </p> </td></tr> <tr valign="top"><td><code>n</code></td> <td> <p> 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</code>; negative values of <code>n</code> will "flip" the value of <code>type</code>, i.e., <code>n=-1</code> and <code>type='lead'</code> is the same as <code>n=1</code> and <code>type='lag'</code>. </p> </td></tr> <tr valign="top"><td><code>fill</code></td> <td> <p> Value to use for padding when the window goes beyond the input length. </p> </td></tr> <tr valign="top"><td><code>type</code></td> <td> <p> default is <code>"lag"</code> (look "backwards"). The other possible values <code>"lead"</code> (look "forwards") and <code>"shift"</code> (behave same as <code>"lag"</code> except given names). </p> </td></tr> <tr valign="top"><td><code>give.names</code></td> <td> <p>default is <code>FALSE</code> which returns an unnamed list. When <code>TRUE</code>, names are automatically generated corresponding to <code>type</code> and <code>n</code>. If answer is an atomic vector, then the argument is ignored. </p> </td></tr> </table> <h3>Details</h3> <p><code>shift</code> accepts vectors, lists, data.frames or data.tables. It always returns a list except when the input is a <code>vector</code> and <code>length(n) == 1</code> in which case a <code>vector</code> 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]</code> would lag every column of <code>.SD</code> by 1 for each group and <code>DT[, newcol := colA + shift(colB)]</code> would assign the sum of two <em>vectors</em> to <code>newcol</code>. </p> <p>Argument <code>n</code> allows multiple values. For example, <code>DT[, (cols) := shift(.SD, 1:2), by=id]</code> would lag every column of <code>.SD</code> by <code>1</code> and <code>2</code> for each group. If <code>.SD</code> contained four columns, the first two elements of the list would correspond to <code>lag=1</code> and <code>lag=2</code> for the first column of <code>.SD</code>, the next two for second column of <code>.SD</code> and so on. Please see examples for more. </p> <p><code>shift</code> is designed mainly for use in data.tables along with <code>:=</code> or <code>set</code>. 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</code> to <code>TRUE</code>. </p> <h3>Value</h3> <p>A list containing the lead/lag of input <code>x</code>. </p> <h3>See Also</h3> <p><code><a href="data.table.html">data.table</a></code> </p> <h3>Examples</h3> <pre> # on vectors, returns a vector as long as length(n) == 1, #1127 x = 1:5 # lag with n=1 and pad with NA (returns vector) shift(x, n=1, fill=NA, type="lag") # lag with n=1 and 2, and pad with 0 (returns list) shift(x, n=1:2, fill=0, type="lag") # getting a window by using positive and negative n: shift(x, n = -1:1) shift(x, n = -1:1, type = "shift", give.names = TRUE) # on data.tables DT = data.table(year=2010:2014, v1=runif(5), v2=1:5, v3=letters[1:5]) # lag columns 'v1,v2,v3' DT by 1 and fill with 0 cols = c("v1","v2","v3") anscols = paste("lead", cols, sep="_") DT[, (anscols) := shift(.SD, 1, 0, "lead"), .SDcols=cols] # return a new data.table instead of updating # with names automatically set DT = data.table(year=2010:2014, v1=runif(5), v2=1:5, v3=letters[1:5]) DT[, shift(.SD, 1:2, NA, "lead", TRUE), .SDcols=2:4] # lag/lead in the right order DT = data.table(year=2010:2014, v1=runif(5), v2=1:5, v3=letters[1:5]) DT = DT[sample(nrow(DT))] # add lag=1 for columns 'v1,v2,v3' in increasing order of 'year' cols = c("v1","v2","v3") anscols = paste("lag", cols, sep="_") DT[order(year), (cols) := shift(.SD, 1, type="lag"), .SDcols=cols] DT[order(year)] # while grouping DT = data.table(year=rep(2010:2011, each=3), v1=1:6) DT[, c("lag1", "lag2") := shift(.SD, 1:2), by=year] # on lists ll = list(1:3, letters[4:1], runif(2)) shift(ll, 1, type="lead") shift(ll, 1, type="lead", give.names=TRUE) shift(ll, 1:2, type="lead") # fill using first or last by group DT = data.table(x=1:6, g=rep(1:2, each=3)) DT[ , shift(x, fill=x[1L]), by=g] DT[ , shift(x, fill=x[.N], type="lead"), by=g] </pre> <hr /><div style="text-align: center;">[Package <em>data.table</em> version 1.14.4 <a href="00Index.html">Index</a>]</div> </body></html>