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
set_labels(x, labels, new_names = NULL, as_factor = TRUE)
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
A character vector of the same length as
x is modified by reference and returned invisibly.
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#> strategy_id grp_id #> 1: 1 1 #> 2: 2 2d2#> strategy_id grp_id #> 1: s1 g1 #> 2: s2 g2d3#> strategy_id grp_id strategy_name grp_name #> 1: 1 1 s1 g1 #> 2: 2 2 s2 g2