I am working with a dataset with haven labelled columns (i.e., variables with labels). The dataset is in wide format, as follows:
df <- data.frame("Trust" = c(1, 2, 3, 2, 3),
"Evaluation" = c(2, 3, 1, 1, 3))
Trust and Evaluation are survey questions. Each row corresponds to an individual and they give an answer in a scale from 1 (No trust/bad evaluation)
to 3 (Absolute trust/good evaluation)
.Trust and Evaluation variables are labelled (i.e., the header in the RStudio viewer appears with a description ("how much do you trust X", "how do you evaluate Y")
. I am not able to add the labels on the example and I cannot share the original dataset, but I think is clear what I mean.
What I want is to convert this into long format, as:
df <- data.frame("Question" = c("Trust", "Trust", "Trust", "Trust", "Trust", "Evaluation", "Evaluation", "Evaluation", "Evaluation"),
"Value" = c(1, 2, 3, 2, 3, 2, 3, 1, 1, 3)),
"Label" = c("LabelTrust", "LabelTrust", "LabelTrust", "LabelTrust", "LabelTrust", "LabelEvaluation", "LabelEvaluation", "LabelEvaluation", "LabelEvaluation")
Ideally, then, I want to create a column for the name of the variable, another for the label and another for the value this variable takes. I apologize because I think the code is not fully reproducible, but I think it is clear what I mean.
Is there any way to make this? I am trying to do this with pivot_longer from tidyr but running into problems because of the labelled nature of the columns. I guess I could make it if I first convert the variables into factors, but then I would lose the labels name and this is not what I want to achieve.
If the solution could come from the tidyverse, that'd be awesome. Thanks everybody so much!