pct_change | R Documentation |
Calculate percent change from one vector (x) to another (y)
pct_change(x, y, denominator = 100)
x |
[ |
y |
[ |
denominator |
[ |
[numeric()
]
Percent change from x to y: denominator * (y - x) / x
# vectors
pct_change(x = c(10, 11, 12), y = c(100, 22, 18))
# data.table
library(data.table)
dt <- data.table(
x = c(10, 11, 12),
y = c(100, 22, 18)
)
dt[, change := pct_change(x, y)]
# percent change within one data.table column
dt <- data.table(
year = c(2000, 2001, 2002),
val = c(10, 11, 12)
)
dt[, change := pct_change(x = shift(val), y = val)]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.