Calculates proportion/percent differences between subsequent (or lagged) elements of a numeric vector.
Controls spacing between proportion/percent 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.
Each proportion/percent difference is based on two numbers, say
x2. The proportion difference is defined as
(x1 - x2) / mean(x1, x2), and the percent difference is the same quantity multiplied by
100. Notice that this is NOT proportion/percent change, which divides by the initial value
x1 rather than the average of
pchanges for the proportion/percent change operation.
This function uses C++ code to achieve a 3-4 times speed increase compared to the base R code:
len <- length(x); p1 <- x[2: len]; p2 <- x[1: (len - 1)]; (p1 - p2) / (0.5 * (p1 + p2)).
Dane R. Van Domelen
Acknowledgment: This material is based upon work supported by the National Science Foundation Graduate Research Fellowship under Grant No. DGE-0940903.
1 2 3 4 5 6 7 8 9 10 11 12 13
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.