statar (version 0.1.3)

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

Description

lead and lag with respect to a time variable

Usage

lead(x, n = 1L, order_by = NULL, along_with = NULL, units = NULL,
  default = NA, ...)

## S3 method for class 'default': lag(x, n = 1L, order_by = NULL, along_with = NULL, units = NULL, 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)
order_by
override the default ordering to use another vector
along_with
use this variable to lag with respect to n along_with rather than n row
units
Deprecated. Use elapsed_dates
default
value used for non-existant rows. Defaults to NA.
...
Needed for compatibility with lag generic.

Examples

Run this code
year <- c(1989, 1991, 1992)
value <- c(4.1, 4.5, 3.3)
lag(value, 1, order_by = year) # returns value in previous year, like  dplyr::lag
lag(value, 1, along_with = year) #  returns value in year - 1
lead(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)
lag(value, along_with = datem)

Run the code above in your browser using DataLab