label factor levels from a lookup table (using labelled?)
03:49 27 Feb 2026

I want to automatically label all my variables from survey data that have corresponding levels and labels in a lookup table.

Here's some dummy data - i have a "dat" table with the raw data, and a "labels" table with the labels.

dummy_dat<- data.frame("X1"=c(1,2,1), "X2_1"=c(1,2,3), "X2_2"=c(3,2,1),"X3"=c(1,2,3))

dummy_labels <- data.frame(question = c("X1", "X2_1", "X2_2", "X3"),
  x1 = c("yes", "never", "never", NA),
  x2 = c("no", "sometimes", "sometimes", NA),
  x3 = c(NA, "always", "always", NA)
)

I've tried melting the labels table and removing the "x" from the colname

melt_dummylab<- reshape2::melt(dummy_labels,id.vars=c("question"))
melt_dummylab$variable<- gsub( "x", "", as.character(melt_dummylab$variable))

and then I tried using the dictionary_to_value function from the labelled package:

dic_test<- melt_dummylab %>% 
  dictionary_to_value_labels(names_from =question, values_from=variable ,labels_from = value, data=dummy_dat)

but then I don't know how to pass it to set_value_label. when I try I get an error "`.data` should be a data frame, a survey design or a vector."

labelled<- set_value_labels(dic_test,labels = dic_test)

If anybody knows how to fix this, or a solution using another package or even base R would be great.

r label survey