ref_expand: Expand a reference data.frame containing N hierarchical...

View source: R/ref_expand.R

ref_expandR Documentation

Expand a reference data.frame containing N hierarchical columns to an N-level reference data.frame

Description

For example, a municipality-level reference data.frame might contain three hierarchical columns — country, state, and municipality — but nonetheless only reflect the municipality level in that all rows represent a unique municipality. The lower-resolution levels (state, country) are implied but not explicitly represented as unique rows. If we wish to allow matches to the lower-resolution levels, we need additional rows specific to these levels.

This function takes a reference data.frame with N hierarchical columns, and adds rows for each unique combination of each level that is not currently explicitly represented.

Usage

ref_expand(ref, pattern, by, lowest_level = 1L)

Arguments

ref

data.frame containing hierarchical columns with reference data

pattern

regex pattern to match the names of the hierarchical columns in ref (supply either pattern or by)

by

vector giving the names of the hierarchical columns in ref (supply either pattern or by)

lowest_level

integer representing the lowest-resolution level (defaults to 1)

Value

A data.frame created by expanding ref to all implied hierarchical levels

Examples

# subset example reference df to the admin-2 level
ne_ref_adm2 <- ne_ref[!is.na(ne_ref$adm2),]

# expand back to all levels
ref_expand(ne_ref_adm2, pattern = "adm", lowest_level = 0)


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