shift_both_ways: Shifts data forward or backward, padding with NA's.

Description Usage Arguments Value Note Author(s) Examples

Description

Convenience function that allows to use both lag() and lead() in one function.

Usage

1
shift_both_ways(x,shift)

Arguments

x

A vector or dataframe that needs to be shifted

shift

A negative or positive integer giving period by which the data are to be shifted.

Value

A vector or dataframe that is shifted by the specified period.

Note

I mostly used this for vectors. The function works on dataframes but efficiency can be improved. I called it shift_both_ways because the data.table package (which this package depends on) already has a function called shift, which (unfortunately) only shifts one way depending on whether you speficy type="lag" or "lead". Just thought this was easier.

Author(s)

Marloes Eeftens, marloes.eeftens@swisstph.ch

Examples

1
2
3
4
5
6
7
#Make up some data
a<-c(1,2,3,NA,5,6,7,NA,9,10)
my_df<-data.frame(A=1:10,B=LETTERS[1:10],C=letters[1:10],stringsAsFactors= F )

#Same as lead(a,1)
shift_both_ways(a,-1)
shift_both_ways(my_df,2)

MarloesEeftens/SenseOfTime documentation built on May 16, 2019, 7:21 a.m.