Apply Labels

apply_labels(
  data,
  dict,
  from = "value",
  to = "value_label",
  by = "variable",
  names_from = "variable",
  names_to = "variable_label",
  dataset_name = NULL,
  ...
)

Arguments

data

dataset to apply labeling on.

dict

dictionary to use for application of labeling.

from

a column name or position defining words or keys to be replaced

to

a column name or position defining replacement values

by

character or integer - which column in dict defines the columns in data corresponding to each section of the dict. Defaults to "variable".

names_from

column name or position defining where to match names from. Defaults to "variable".

names_to

column name or position defining replacements for names(data). Defaults to "variable_label".

dataset_name

(optional) name of dataset to filter dict by

...

Passed to match_df

Value

a labelled, display friendly tibble

Examples

dictionary <- data.frame( var = c(rep("x1", 2), rep("x2", 3), rep("x3", 5)), var_lab = c(rep("Column 1", 2), rep("Column 2", 3), rep("Column 3", 5)), val = c(c(TRUE, FALSE), c(1:3), letters[1:5]), val_lab = c(c("YES", "NO"), paste0("Group ", c(1:3)), paste0("Area ", LETTERS[1:5])) ) dat <- data.frame( "x1" = rep(c(TRUE, FALSE), 15), "x2" = rep(c(1:3), 10), "x3" = rep(letters[1:5], 6) ) apply_labels(dat, dictionary, from = "val", to = "val_lab", by = "var", names_from = "var", names_to = "var_lab")
#> Column 1 Column 2 Column 3 #> 1 YES Group 1 Area A #> 2 NO Group 2 Area B #> 3 YES Group 3 Area C #> 4 NO Group 1 Area D #> 5 YES Group 2 Area E #> 6 NO Group 3 Area A #> 7 YES Group 1 Area B #> 8 NO Group 2 Area C #> 9 YES Group 3 Area D #> 10 NO Group 1 Area E #> 11 YES Group 2 Area A #> 12 NO Group 3 Area B #> 13 YES Group 1 Area C #> 14 NO Group 2 Area D #> 15 YES Group 3 Area E #> 16 NO Group 1 Area A #> 17 YES Group 2 Area B #> 18 NO Group 3 Area C #> 19 YES Group 1 Area D #> 20 NO Group 2 Area E #> 21 YES Group 3 Area A #> 22 NO Group 1 Area B #> 23 YES Group 2 Area C #> 24 NO Group 3 Area D #> 25 YES Group 1 Area E #> 26 NO Group 2 Area A #> 27 YES Group 3 Area B #> 28 NO Group 1 Area C #> 29 YES Group 2 Area D #> 30 NO Group 3 Area E