Learn R Programming

datapackage (version 0.2.0)

dp_to_code: 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)

Value

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

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.

See Also

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

Examples

Run this code
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")


Run the code above in your browser using DataLab