Computes the percent change from start_value
to end_value
:
$$(end - start) / start$$
Details
Vectorized for tidyverse workflows (e.g., dplyr::mutate()
).
Examples
percent_change(100, 120) # 0.20 (20% increase)
#> [1] 0.2
percent_change(c(100, 80), c(110, 72)) # c(0.10, -0.10)
#> [1] 0.1 -0.1
percent_change(c(100, NA), c(110, 120)) # c(0.10, NA)
#> Warning: NA values detected; returning NA for those positions.
#> [1] 0.1 NA
percent_change(0, 5) # NA with a warning (division by zero)
#> Warning: Division by zero in `start_value`; returning NA for those positions.
#> [1] NA