# diffs: Lagged Differences (Alternate Implementation) In stocks: Stock Market Analysis

## Description

Calculates differences between subsequent (or lagged) elements of a numeric vector. Very similar to base function `diff`, but written in C++ to run faster.

## Usage

 `1` ```diffs(x, lag = 1) ```

## Arguments

 `x` Numeric vector. `lag` Controls spacing between differences. For example, lag of 1 means you want to calculate differences between elements 1 and 2, 2 and 3, 3 and 4, and so on; a lag of 2 means you want calculate differences between elements 1 and 3, 2 and 4, 3 and 5, and so on.

Numeric vector.

## Note

This function uses C++ code to achieve an approximate 4 times speed increase compared to the base R function `diff`.

## Author(s)

Dane R. Van Domelen

## References

Acknowledgment: This material is based upon work supported by the National Science Foundation Graduate Research Fellowship under Grant No. DGE-0940903.

`pdiffs`, `pchanges`, `ratios`
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21``` ```# Randomly generate 1 million values from a 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(y == z) # But diffs is faster benchmark(y = diffs(x)) benchmark(z = diff(x)) # diffs also faster than diff for 2-lag difference x <- rnorm(100000) y <- diffs(x, 2) z <- diff(x, 2) all(y == z) benchmark(y = diffs(x)) benchmark(z = diff(x)) ```