Adds a column giving each country's value as a share of the (year's) world
total – e.g. share of global emissions or GDP. Operates within year when a
panel is supplied. A dplyr grouping on data is ignored – the denominator
is always the world (or the year's) total, never the group's.
Value
data with a share column added: a proportion in [0, 1] when no
value is negative. Negative values are the caller's business and are
summed as they are, so their shares fall outside that range.
Examples
df <- data.frame(iso3c = c("USA", "CHN"), co2 = c(5, 10))
share_of_world(df, co2)
#> # A tibble: 2 × 3
#> iso3c co2 co2_share
#> <chr> <dbl> <dbl>
#> 1 USA 5 0.333
#> 2 CHN 10 0.667
