Learn R Programming

mosaic (version 0.8-1)

ediff: Lagged Differences with equal length

Description

Often when creating lagged differences, it is awkward that the differences vector is shorter than the original. ediff pads with NAs to make its output the same length as the input.

Usage

ediff(x, lag = 1, differences = 1, frontPad = TRUE, ...)

Arguments

x
a numeric vector or a matrix containing the values to be differenced
lag
an integer indicating which lag to use
differences
an integer indicating the order of the difference
frontPad
logical indicating whether padding is on the front (default) or back end.
...
further arguments to be passed to or from methods

See Also

diff since ediff is a thin wrapper around diff.

Examples

Run this code
ediff(1:10)
ediff(1:10, 2)
ediff(1:10, 2, 2)
x <- cumsum(cumsum(1:10))
ediff(x, lag = 2)
ediff(x, differences = 2)
ediff(.leap.seconds)
Men <- subset(SwimRecords, sex=="M")
Men <- transform(Men, change=ediff(time), interval=ediff(year))
head(Men)

Run the code above in your browser using DataLab