base_match: Defines factors using value-label mapping

View source: R/base_match.r

base_matchR Documentation

Defines factors using value-label mapping

Description

A base-R approximation of case_match() from 'dplyr'. Unlike case_match(), base_when() returns a factor. The levels will be ordered according to the order included in ... (see below).

Usage

base_match(original_variable, ..., as_factor = TRUE, string_for_na = "")

Arguments

original_variable

the original variable

...

the codebook, specified as a named vector, with each element in 'label'=value format, with the levels listed in the desired order

as_factor

logical, controlling whether the function should return a factor

string_for_na

string value that will be converted to NA

Value

a factor

Examples

# load data
data(nhanes)

# define country
nhanes<-nhanes |> transform(
  country=base_match(dmdborn4,'USA'=1,'Other'=2)
)

baseverse documentation built on April 29, 2026, 1:08 a.m.