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.
Arguments
- data
A panel with
iso3candyear.- value
The value column (unquoted).
- type
"yoy"(default, period-over-period) or"cagr"(compound annual growth rate vs. the first non-NAyear)."cagr"needs a positive ratio at both ends, so a negative value givesNAfor that row and a non-positive or infinite base year givesNAfor that country, each with a warning; a value of exactly0is 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 isNA(with a warning) rather thanInfor -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, andNAwhere 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
