Apply value labels from CSV file

Hello everyone!

I have a problem with applying value labels to a dataset, from a csv-file called "labels". When I import the csv-file "labels", the object looks like this in RStudio (with only the 10 first rows, and some information censored):

image

I would like some R code that can apply these labels automatically to the dataset "dataset", as I often download csv-files in these formats. I have tried many different solutions (with the help of ChatGPT), without success. So far my code looks like this:

vaerdi_labels <- read.csv("labels.csv", sep = ";", stringsAsFactors = FALSE, header = FALSE)
for (i in 1:nrow(vaerdi_labels)) {
var_name <- vaerdi_labels[i, 1]
var_value <- vaerdi_labels[i, 2]
value_label <- vaerdi_labels[i, 3]
val_label(dataset[[var_name]], var_value) <- value_label
}

When I run the code, I get the following error:

Error in vec_cast_named():
! Can't convert labels to match type of x .
Run rlang::last_trace() to see where the error occurred.
Error in exists(cacheKey, where = .rs.WorkingDataEnv, inherits = FALSE) :
invalid first argument
Error in assign(cacheKey, frame, .rs.CachedDataEnv) :
attempt to use zero-length variable name

When applying variable labels to the dataset "dataset", I use the following code, which works perfectly:

variabel_labels <- read.csv("variables.csv", sep = ";", stringsAsFactors = FALSE)
for (i in 1:nrow(variabel_labels)) {
var_name <- variabel_labels[i, 1]
var_label <- variabel_labels[i, 2]
label(dataset[[var_name]]) <- var_label
}

I've tried using a similar solution when applying value labels, but it doesn't work. Is there a smart solution to my problem?

Kind regards

It would help to have a small example of what a dataset might look like before and after doing whatever it is you are doing.

I think this is what you're looking for:

library(labelled)

labels <- data.frame(
  V1 = c("survey", "organize", "stat_1", "stat_1", "stat_2", "stat_2", "stat_3", "stat_3", "stat_4", "stat_4"),
  V2 = c("censored_1", "censored_2", 0, 1, 0, 1, 0, 1, 0, 1),
  V3 = c("respondent questionaire", "censored_3", "ikke vaigt", "vaigt", "ikke vaigt", "vaigt", "ikke vaigt", "vaigt", "ikke vaigt", "vaigt")
)

df <- data.frame(
  name = letters,
  value = sample(0:1, 26, replace = TRUE)
)

for (i in 1:nrow(labels)) {
  var_name = labels[i,1]
  var_value = as.numeric(labels[i,2])
  value_label = labels[i, 3]
  if(!is.na(var_value)) {val_label(df$value, var_value) <- value_label}
}