if (FALSE) {
dat <- data.frame(id = 1:5,
gender = c(NA, 0, 1, 1, 0),
age = c(16, 19, 17, NA, 16),
status = c(1, 2, 3, 1, 4),
score = c(511, 506, 497, 502, 491))
# Example 1: Write SPSS file using the haven package
write.sav(dat, file = "Dataframe_haven.sav")
# Example 2: Write SPSS file using PSPP,
# write CSV file and SPSS syntax along with the SPSS file
write.sav(dat, file = "Dataframe_PSPP.sav", pspp.path = "C:/Program Files/PSPP",
write.csv = TRUE, write.sps = TRUE)
# Example 3: Specify variable attributes
# Note that it is recommended to manually specify the variables attritbues in a CSV or
# Excel file which is subsequently read into R
attr <- data.frame(# Variable names
var = c("id", "gender", "age", "status", "score"),
# Variable labels
label = c("Identification number", "Gender", "Age in years",
"Migration background", "Achievement test score"),
# Value labels
values = c("", "0 = female; 1 = male", "",
"1 = Austria; 2 = former Yugoslavia; 3 = Turkey; 4 = other",
""),
# User-missing values
missing = c("", "-99", "-99", "-99", "-99"), stringsAsFactors = FALSE)
# Example 4: Write SPSS file with variable attributes using the haven package
write.sav(dat, file = "Dataframe_haven_Attr.sav", var.attr = attr)
# Example 5: Write SPSS with variable attributes using PSPP
write.sav(dat, file = "Dataframe_PSPP_Attr.sav", var.attr = attr,
pspp.path = "C:/Program Files/PSPP")
}
Run the code above in your browser using DataLab