Skip to contents

Convenience wrapper that appends a region column (and optionally a subregion) to a data frame by looking up an existing country column with recode_region().

Usage

add_region(
  data = NULL,
  country,
  region_to = "region",
  subregion = FALSE,
  quiet = FALSE
)

Arguments

data

A data frame.

country

The country column (unquoted).

region_to

Name of the region column to add. Defaults to "region".

subregion

If TRUE, also add a subregion column. Defaults to FALSE.

quiet

Passed to recode_region().

Value

data with the new column(s) added.

Details

This is the data-frame-friendly form of recode_region(): point it at an existing country column and it appends a region column (named via region_to) and, optionally, a subregion. Handy right after read_folder() to enrich raw exports before grouping by region. The lookup, matching and unmatched-country warning behave exactly as in recode_region().

Examples

df <- tibble::tibble(demo_country = c("Germany", "Japan", "Brazil"))
add_region(df, demo_country, subregion = TRUE)
#> # A tibble: 3 × 3
#>   demo_country region        subregion     
#>   <chr>        <chr>         <chr>         
#> 1 Germany      Europe        Western Europe
#> 2 Japan        Asia          East Asia     
#> 3 Brazil       South America South America