shift: Shift a vector an arbitrary amount

Description Usage Arguments Note Author(s) Source Examples

View source: R/shift.R

Description

Shift a variable by an amount, and pad with NA or your value of choice.

Usage

1
shift(x, offset = 1, pad = NA)

Arguments

x

Vector to be shifted

pad

Value to use for padding

shift

Quantity to shift (forward or backward)

Note

Can shift forward or backwards

Author(s)

Gene Leynes (originally dnlbrky http://stackoverflow.com/users/1344789/dnlbrky)

Source

http://stackoverflow.com/questions/14689424/use-a-value-from-the-previous-row-in-an-r-data-table-calculation

Examples

 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

geneorama/geneorama documentation built on Oct. 17, 2020, 12:35 a.m.