write.sav | R Documentation |
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.
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)
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., |
var.attr |
a matrix or data frame with variable attributes used in the
SPSS file, only 'variable labels' (column name |
pspp.path |
a character string indicating the path where the PSPP folder
is located on the computer, e.g. |
digits |
an integer value indicating the number of decimal places shown in the SPSS file for non-integer variables. |
write.csv |
logical: if |
sep |
a character string for specifying the CSV file, either |
na |
a character string for specifying missing values in the CSV file. |
write.sps |
logical: if |
check |
logical: if |
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
.
Part of the function using PSPP was adapted from the write.pspp()
function in the miceadds package by Alexander Robitzsch, Simon Grund and
Thorsten Henke (2019).
Takuya Yanagida takuya.yanagida@univie.ac.at
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.
read.sav
, write.xlsx
, write.dta
,
write.mplus
## Not run:
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")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.