set_labels: Set value labels

View source: R/hesim_data.R

set_labelsR Documentation

Set value labels

Description

Update existing variables or create new ones that replace existing values with more informative labels as in factor(). All modifications are performed by reference (see data.table::set() for more information about assignment by reference).

Usage

set_labels(x, labels, new_names = NULL, as_factor = TRUE)

Arguments

x

A data.table.

labels

A list of named vectors containing the values and labels of variables. The elements of each vector are the values of a variable and the names are the labels. The names of the list are the names of the variables. See the output returned by get_labels() for an example.

new_names

A character vector of the same length as labels where each element denotes the name of a new variable to create for the corresponding element in labels. If NULL, then the variables in labels are modified and no new variables are created; otherwise, the existing variables are not modified and new variables are created instead.

as_factor

If TRUE factor variables are created; otherwise character vectors are created.

Value

x is modified by reference and returned invisibly.

See Also

get_labels()

Examples

library("data.table")
labs <- list("strategy_id" = c("s1" = 1, 
                               "s2" = 2),
            "grp_id" = c("g1" = 1, 
                         "g2" = 2))
d1 <- data.table(strategy_id = 1:2, grp_id = 1:2)
d2 <- copy(d1); d3 <- copy(d2)
set_labels(d2, labels = labs)
set_labels(d3, labels = labs, new_names = c("strategy_name", "grp_name"))
d1
d2
d3

hesim documentation built on Sept. 4, 2022, 1:06 a.m.