
Add a granularity_geo column to a data set
Source:R/add_variable_to_data_set.R
add_granularity_geo_to_data_set.RdDerives the geographic granularity label from the location_code column
and adds it as a new granularity_geo column, modifying x in place.
When location_reference is NULL the granularity is inferred from the
location code prefix; when a reference table is supplied, it is looked up
directly.
Examples
library(data.table)
#>
#> Attaching package: ‘data.table’
#> The following object is masked from ‘package:base’:
#>
#> %notin%
data <- data.table(location_code = c("nation_nor", "county_nor03", "blah"))
csdata::add_granularity_geo_to_data_set(data)
print(data)
#> location_code granularity_geo
#> <char> <char>
#> 1: nation_nor nation
#> 2: county_nor03 county
#> 3: blah blah
library(data.table)
data <- data.table(location_code = c("nation_nor", "county_nor03", "blah"))
csdata::add_granularity_geo_to_data_set(data, location_reference = csdata::nor_locations_names())
print(data)
#> location_code granularity_geo
#> <char> <char>
#> 1: nation_nor nation
#> 2: county_nor03 county
#> 3: blah <NA>