diffs {stocks} | R Documentation |
Lagged Differences (Alternate Implementation)
Description
Calculates differences between subsequent (or lagged) elements of a vector.
Very similar to diff
, but written in C++.
Usage
diffs(x, lag = 1L)
Arguments
x |
Numeric vector. |
lag |
Numeric value (e.g. 2 for differences between 1st and 3rd element, 2nd and 4th, ...). |
Value
Numeric vector.
Examples
# Generate 1 million values from Poisson(3) distribution
x <- rpois(100000, 3)
# Calculate vector of differences between subsequent values
y <- diffs(x)
# Could get same result from base R function diff
z <- diff(x)
all.equal(y, z)
# But diffs is faster
benchmark(diffs(x), diff(x), replications = 100)
[Package stocks version 1.1.4 Index]