Skip to contents

en_characters_to_factors() converts every column named by a declared set to a factor whose levels are that set; en_factors_to_characters() converts factor columns back to character. The pair is what the storage layer uses at the file boundary (see the storage_dictionary option), and it is exported because a caller reading a store directly – rather than through getData() or obj2mem() – has to do the same.

Usage

en_characters_to_factors(x, scenario, verbose = isVerbose())

en_factors_to_characters(x, scenario = NULL)

Arguments

x

a data.frame (or data.table/tibble).

scenario

a scenario, a modInp, or a named list of sets. For en_factors_to_characters() it is optional and only narrows which columns are converted; NULL converts every factor column.

verbose

logical, report values found outside their declared set.

Value

x with the set columns converted. The class of the input is kept.

Details

Only a column whose NAME is a set of the problem is encoded, and only when it is character, so a numeric set such as year is left alone. The levels are the declared set rather than the column's distinct values, so the codes mean the same thing in every table and a join between two tables compares integer codes instead of reconciling two level sets. A value outside the declared set would become NA, so it is kept as a level instead and reported once.

Examples

if (FALSE) { # \dontrun{
d <- getData(scen, "pDemand", merge = TRUE)
f <- en_characters_to_factors(d, scen)   # region, timeslice as dictionaries
en_factors_to_characters(f)              # back again
} # }