dp_to_code: Recode a variable to 'code' using the associated categories

View source: R/dp_to_code.R

dp_to_codeR Documentation

Recode a variable to code using the associated categories

Description

Recode a variable to code using the associated categories

Usage

dp_to_code(x, categorieslist = dp_categorieslist(x), ..., warn = FALSE)

Arguments

x

the variable to recode

categorieslist

a data.frame with the categories as a data.frame.

...

passed on to as.codelist.

warn

give a warning when there is no code list.

Details

Uses the code method from the 'codelist' package. This package therefore needs to be installed. See the documentation of that package for how to work with 'code' objects.

By setting the option 'DP_TRIM_CODE' to TRUE white space at the beginning and end of character values will be removes from the category values and from x. Also multiple hyphens at the beginning character values will be removed This can be disabled by setting the option 'DP_TRIM_HYPHEN' to FALSE.

Value

Returns a 'code' object or x when no categories could be found (categorieslist = NULL).

See Also

An alternative is the dp_to_factor function to convert to regular R factor.

Examples

fn <- system.file("examples/iris", package = "datapackage")
dp <- open_datapackage(fn)
dta <- dp |> dp_get_data("complex", convert_categories = "no")
dp_to_code(dta$factor1)

dp |> dp_get_data("complex", convert_categories = "dp_to_code")



djvanderlaan/datapackage documentation built on June 12, 2025, 2:44 a.m.