I'm collaborating on a project with some people who only use SPSS although I use R. I'm trying to create a dataframe in R with variable labels and value labels for factor variables to export to SPSS. However, once I open the file in SPSS, I get the variable names but not the value names.
Does anyone has any idea how to achieve this?
Here what I've tried so far. mtcars
example.
mtcars = modify(mtcars,{
var_lab(mpg) = "Miles/(US) gallon"
var_lab(cyl) = "Number of cylinders"
var_lab(disp) = "Displacement (cu.in.)"
var_lab(hp) = "Gross horsepower"
var_lab(drat) = "Rear axle ratio"
var_lab(wt) = "Weight (lb/1000)"
var_lab(qsec) = "1/4 mile time"
var_lab(vs) = "V/S"
var_lab(am) = "Transmission (0 = automatic, 1 = manual)"
val_lab(am) = c(automatic = 0, manual=1)
var_lab(gear) = "Number of forward gears"
var_lab(carb) = "Number of carburetors"
})
spss_data <- haven::write_sav(mtcars, "test_mtcars.sav")
UPDATE
I'm able to export value labels into SPSS using the package haven
, however the variable cannot be factor or character otherwise the function doesn't work. This is a problem since these variables are factor. They should be factor in R and nominal in SPSS.
mtcars$am <- labelled(mtcars$am, c(automatic = 0, manual = 1), label = "Transmission")