Skip to contents

Adds a growth-rate column to a panel: either the period-over-period change ("yoy") or the compound annual growth rate from the first observed year ("cagr"), computed per country.

Usage

growth_rate(
  data,
  value,
  type = c("yoy", "cagr"),
  suffix = "_growth",
  by = c("year", "row")
)

Arguments

data

A panel with iso3c and year.

value

The value column (unquoted).

type

"yoy" (default, period-over-period) or "cagr" (compound annual growth rate vs. the first non-NA year). "cagr" needs a positive ratio at both ends, so a negative value gives NA for that row and a non-positive or infinite base year gives NA for that country, each with a warning; a value of exactly 0 is a legitimate annualised -100%. "yoy" is a plain ratio change and is defined for negative values, but not after a zero or an infinity: neither has a ratio, so that row is NA (with a warning) rather than Inf or -100%.

suffix

Suffix for the new column (default "_growth").

by

How the previous value is found for "yoy". "year" (default) takes the same country's value one year earlier, and NA where that year is absent, so a gap in the panel can never pass for a one-year change. "row" takes the previous observation, whatever its year – the 3.0.0 behaviour, for a panel that is irregular by design – and warns when the years are not consecutive. A period column whose labels are not years ("pre-war") needs "row". "cagr" always divides by the actual span of years.

Value

data with a growth-rate column added (a proportion, so 0.03 = 3%). Rows come back sorted by iso3c then year: the calculation reads each country's series in time order, so a row-aligned vector held alongside data will no longer line up.

Examples

df <- data.frame(iso3c = "USA", year = 2000:2002, gdp = c(100, 110, 121))
growth_rate(df, gdp)
#> # A tibble: 3 × 4
#>   iso3c  year   gdp gdp_growth
#>   <chr> <int> <dbl>      <dbl>
#> 1 USA    2000   100     NA    
#> 2 USA    2001   110      0.100
#> 3 USA    2002   121      0.100