mosaic (version 0.14.4)

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, pad = c("head", "tail", "symmetric"),
  frontPad, ...)

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

pad

one of "head", "tail", or "symmetric". indicating where the NA padding should be added to the result.

frontPad

logical indicating whether padding is on the front (head) or back (tail) end. This exists for backward compatibility. New code should use pad instaed.

further arguments to be passed to or from methods

See Also

diff since ediff is a thin wrapper around diff.

Examples

Run this code
# NOT RUN {
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(x, differences = 2, pad="symmetric")
ediff(.leap.seconds)
if (require(mosaicData)) {
Men <- subset(SwimRecords, sex=="M")
Men <- mutate(Men, change=ediff(time), interval=ediff(year))
head(Men) 
}
# }

Run the code above in your browser using DataCamp Workspace