statar (version 0.1.1)

lead-lag: lead and lag.

Description

lead and lag are useful for comparing values for date offset by a constant

Usage

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

## S3 method for class 'default': lag(x, n = 1L, order_by = NULL, units = NULL, along_with = 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
units
A character when along_with is a date (one of "second", "minute", "hour", "day", "week", "month", "quarter", "year").
along_with
compute lag with respect to this vector instead of previous row
default
value used for non-existant rows. Defaults to NA.
...
Needed for compatibility with lag generic.

Examples

Run this code
year <- c(1992, 1989, 1991)
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

library(lubridate)
date <- mdy(c("04/03/1992", "01/04/1992", "03/15/1992"))
value <- c(4.1, 4.5, 3.3, 5.3)
datem <- floor_date(date, "month")
value_l <- lag(value, units = "month", along_with = datem)

Run the code above in your browser using DataLab