diffs: Lagged Differences (Alternate Implementation)

Description Usage Arguments Value Examples

View source: R/RcppExports.R

Description

Calculates differences between subsequent (or lagged) elements of a vector. Very similar to diff, but written in C++.

Usage

1
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 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)

vandomed/stocks documentation built on Sept. 2, 2018, 10:10 a.m.