statar (version 0.4.0)

tlead-tlag: lead and lag with respect to a time variable

Description

lead and lag with respect to a time variable

Usage

tlead(x, n = 1L, along_with, default = NA)

tlag(x, n = 1L, along_with, default = NA)

Arguments

x
a vector of values
n
a positive integer of length 1, giving the number of positions to lead or lag by. When the package lubridate is loaded, it can be a period when using with along_with (see the lubridate function minutes, hours, days, weeks, months and years)
along_with
time variable
default
value used for non-existant rows. Defaults to NA.

Examples

Run this code
year <- c(1989, 1991, 1992)
value <- c(4.1, 4.5, 3.3)
tlag(value, 1, along_with = year) #  returns value in year - 1
tlead(value, 1, along_with = year)
library(lubridate)
date <- mdy(c("01/04/1992", "03/15/1992", "04/03/1992"))
value <- c(4.1, 4.5, 3.3)
datem <- as.monthly(date)
tlag(value, along_with = datem)

Run the code above in your browser using DataCamp Workspace