pct_change: Calculate percent change

View source: R/change.R

pct_changeR Documentation

Calculate percent change

Description

Calculate percent change from one vector (x) to another (y)

Usage

pct_change(x, y, denominator = 100)

Arguments

x

[numeric()]
Baseline value(s)

y

[numeric()]
Comparison value(s)

denominator

[numeric()]
Default to 100 for percent (ex: 20%). Specify denominator = 1 to get percent change as a decimal (ex: 0.20).

Value

[numeric()]
Percent change from x to y: denominator * (y - x) / x

Examples

# 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)]


ihmeuw-demographics/demUtils documentation built on Feb. 27, 2024, 1:25 p.m.