diff_n: Lagged Differences for Numeric Values

Description Usage Arguments Value Examples

View source: R/RcppExports.R

Description

Written in C++, this function should always run faster than diff for calculating lagged differences for a numeric vector. For integer vectors, diff_i should run even faster. even faster.

Usage

1
diff_n(x, lag = 1L)

Arguments

x

Numeric vector.

lag

Integer value.

Value

Numeric vector.

Examples

1
2
3
4
5
6
7
8
9
# In general, diff_n is much faster than diff
x <- rnorm(1000)
all.equal(diff(x, 2), diff_n(x, 2))
benchmark(diff(x, 2), diff_n(x, 2), replications = 2000)

# For integer vectors, diff_i should be even faster
x <- rpois(1000, lambda = 5)
all.equal(diff(x, 2), diff_i(x, 2))
benchmark(diff(x, 2), diff_n(x, 2), diff_i(x, 2), replications = 2000)

dvmisc documentation built on May 2, 2019, 5:51 p.m.