Learn R Programming

misty (version 0.6.7)

write.sav: Write SPSS File

Description

This function writes a data frame or matrix into a SPSS file by either using the write_sav() function in the haven package by Hadley Wickham and Evan Miller (2019) or the free software PSPP.

Usage

write.sav(x, file = "SPSS_Data.sav", var.attr = NULL, pspp.path = NULL,
          digits = 2, write.csv = FALSE, sep = c(";", ","), na = "",
          write.sps = FALSE, check = TRUE)

Arguments

x

a matrix or data frame to be written in SPSS, vectors are coerced to a data frame.

file

a character string naming a file with or without file extension '.sav', e.g., "SPSS_Data.sav" or "SPSS_Data".

var.attr

a matrix or data frame with variable attributes used in the SPSS file, only 'variable labels' (column name label), 'value labels' column name values, and 'user-missing values' column name missing are supported (see 'Details').

pspp.path

a character string indicating the path where the PSPP folder is located on the computer, e.g.C:/Program Files/PSPP/.

digits

an integer value indicating the number of decimal places shown in the SPSS file for non-integer variables.

write.csv

logical: if TRUE, CSV file is written along with the SPSS file.

sep

a character string for specifying the CSV file, either ";" for the separator and "." for the decimal point (default, i.e. equivalent to write.csv2) or "." for the decimal point and "," for the separator (i.e. equivalent to write.csv), must be one of both ";" (default) or ",".

na

a character string for specifying missing values in the CSV file.

write.sps

logical: if TRUE, SPSS syntax is written along with the SPSS file when using PSPP.

check

logical: if TRUE, variable attributes specified in the argument var.attr is checked.

Author

Takuya Yanagida takuya.yanagida@univie.ac.at

Details

If arguments pspp.path is not specified (i.e., pspp.path = NULL), write_sav() function in the haven is used. Otherwise the object x is written as CSV file, which is subsequently imported into SPSS using the free software PSPP by executing a SPSS syntax written in R. Note that PSPP needs to be installed on your computer when using the pspp.path argument.

A SPSS file with 'variable labels', 'value labels', and 'user-missing values' is written by specifying the var.attr argument. Note that the number of rows in the matrix or data frame specified in var.attr needs to match with the number of columns in the data frame or matrix specified in x, i.e., each row in var.attr represents the variable attributes of the corresponding variable in x. In addition, column names of the matrix or data frame specified in var.attr needs to be labeled as label for 'variable labels, values for 'value labels', and missing for 'user-missing values'.

Labels for the values are defined in the column values of the matrix or data frame in var.attr using the equal-sign (e.g., 0 = female) and are separated by a semicolon (e.g., 0 = female; 1 = male).

User-missing values are defined in the column missing of the matrix or data frame in var.attr, either specifying one user-missing value (e.g., -99) or more than one but up to three user-missing values separated by a semicolon (e.g., -77; -99.

References

GNU Project (2018). GNU PSPP for GNU/Linux (Version 1.2.0). Boston, MA: Free Software Foundation. https://www.gnu.org/software/pspp/

Wickham H., & Miller, E. (2019). haven: Import and Export 'SPSS', 'Stata' and 'SAS' Files. R package version 2.2.0.

Robitzsch, A., Grund, S., & Henke, T. (2019). miceadds: Some additional multiple imputation functions, especially for mice. R package version 3.4-17.

See Also

read.sav, write.xlsx, write.dta, write.mplus

Examples

Run this code
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