View source: R/separate_hcode.R
separate_hcode | R Documentation |
Separate a data frame column containing hierarchical codes into multiple columns, one for each level within the hierarchical code.
Like tidyr::separate
except that successive levels are cumulative rather
then independent. E.g. the code "canada__ontario__toronto" would be split
into three levels:
"canada"
"canada__ontario"
"canada__ontario__toronto"
separate_hcode(
x,
col,
into,
sep = "__",
extra = c("warn", "drop"),
remove = FALSE
)
x |
|
col |
Name of the column within |
into |
Vector of column names to separate |
sep |
Separator between levels in the hierarchical codes. Defaults to "__". |
extra |
What to do if a hierarchical code contains more levels than are
implied by argument
|
remove |
Logical indicating whether to remove |
The original data.frame x
with additional columns for each level of the
hierarchical code
data(ne_ref)
# generate pcode
ne_ref$pcode <- hcodes_str(ne_ref, pattern = "^adm\\d")
# separate pcode into constituent levels
separate_hcode(
ne_ref,
col = "pcode",
into = c("adm0_pcode", "adm1_pcode", "adm2_pcode")
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.