Learn R Programming

datapackage (version 0.2.0)

dp_to_factor: Recode a variable to factor using the associated categories

Description

Recode a variable to factor using the associated categories

Usage

dp_to_factor(x, categorieslist = dp_categorieslist(x), warn = TRUE)

Value

Returns a factor vector 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.

warn

give a warning when there is no code list.

Details

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

See Also

An alternative is the dp_to_code function to convert to 'code' object from the 'codelist' package.

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_factor(dta$factor1)

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

Run the code above in your browser using DataLab