The two panel primitives everyone hand-rolls (and gets subtly wrong when the
frame isn't sorted): the value n years back, and the change since then –
grouped by iso3c, ordered by year, so country A's 1960 never leaks into
country B's first row.
Usage
lag_by_country(data, value, n = 1, suffix = NULL)
diff_by_country(data, value, n = 1, suffix = NULL)Examples
df <- data.frame(iso3c = "USA", year = 2000:2003, gdp = c(100, 110, 121, 133))
lag_by_country(df, gdp)
#> # A tibble: 4 × 4
#> iso3c year gdp gdp_lag
#> <chr> <int> <dbl> <dbl>
#> 1 USA 2000 100 NA
#> 2 USA 2001 110 100
#> 3 USA 2002 121 110
#> 4 USA 2003 133 121
diff_by_country(df, gdp)
#> # A tibble: 4 × 4
#> iso3c year gdp gdp_diff
#> <chr> <int> <dbl> <dbl>
#> 1 USA 2000 100 NA
#> 2 USA 2001 110 10
#> 3 USA 2002 121 11
#> 4 USA 2003 133 12
