separate_hcode: Separate a hierarchical code reflecting multiple levels into...

View source: R/separate_hcode.R

separate_hcodeR Documentation

Separate a hierarchical code reflecting multiple levels into its constituent parts, with one column for each level

Description

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:

  1. "canada"

  2. "canada__ontario"

  3. "canada__ontario__toronto"

Usage

separate_hcode(
  x,
  col,
  into,
  sep = "__",
  extra = c("warn", "drop"),
  remove = FALSE
)

Arguments

x

data.frame containing a column with hierarchical codes

col

Name of the column within x containing hierarchical codes.

into

Vector of column names to separate col into

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 into.

  • "warn" (the default): emit a warning and drop extra values

  • "drop": drop any extra values without a warning

remove

Logical indicating whether to remove col from the output. Defaults to FALSE.

Value

The original data.frame x with additional columns for each level of the hierarchical code

Examples

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")
)


epicentre-msf/hmatch documentation built on Nov. 15, 2023, 1:47 a.m.