I imported a CSV from an online questionnaire and the platform used to apply it makes available a R syntax file, where categories values/labels and variable labels are specified (which is great). The latter are defined as attributes(data)$variable.labels[1] and this information is saved as an attribute in the data.frame, instead of being assigned to columns.
The problem(s):
- In R: as soon as I change the data, the variable labels are lost (it's a minor problem, I can live without them).
- In SPSS: even if I don't change the data.frame in R, when I export the data using
haven::write_sav(data,"data.sav")the variable labels are lost (major problem as other people need to have this information in SPSS)
I guess that both problems would be solved if the labels are assigned to columns (please tell me if I'm wrong), as happens when the data is imported from SPSS itself (which is not the case here), but I could not do it.
So, I need help to:
- Create a named vector from the (non-named) vector
attr(data, "variable.labels"), so I could try the solution to R: Assign variable labels of data frame columns (Note: I don't want to manually define labels as the purpose of using a prepared syntax file would be partially lost)
or, in alternative,
- Export
attributes(data)$variable.labelsto SPSS (i.e., make these readable by SPSS).
Any other solutions to my problem will be welcomed
Thank you