write.pspp: Writing a Data Frame into SPSS Format Using PSPP Software

write.psppR Documentation

Writing a Data Frame into SPSS Format Using PSPP Software

Description

Writes a data frame into SPSS format using the PSPP software. To use this function, download and install PSPP at first: http://www.gnu.org/software/pspp/pspp.html.

Usage

write.pspp(data, datafile, pspp.path, decmax=6,
   as.factors=TRUE, use.bat=FALSE)

Arguments

data

Data frame

datafile

Name of the output file (without file ending)

pspp.path

Path where the PSPP executable is located, e.g. "C:/Program Files (x86)/PSPP/bin/"

decmax

Maximum number of digits after decimal

as.factors

A logical indicating whether all factors and string entries should be treated as factors in the output file.

use.bat

A logical indicating whether PSPP executed via a batch file in the DOS mode (TRUE) or directly invoked via the system command from within R (FALSE).

Value

A dataset in sav format (SPSS format).

Author(s)

The code was adapted from https://stat.ethz.ch/pipermail/r-help/2006-January/085941.html

See Also

See also foreign::write.foreign.

For writing sav files see also haven::write_sav and sjlabelled::write_spss.

For convenient viewing sav files we recommend the freeware program ViewSav, see http://www.asselberghs.dds.nl/stuff.htm.

Examples

## Not run: 
#############################################################################
# EXAMPLE 1: Write a data frame into SPSS format
#############################################################################

#****
# (1) define data frame
data <- data.frame( "pid"=1000+1:5, "height"=round(rnorm( 5 ),4),
                "y"=10*c(1,1,1,2,2), "r2"=round( rnorm(5),2),
                "land"=as.factor( c( rep("A",1), rep("B", 4 ) ) ) )
#****
# (2) define variable labels
v1 <- rep( "", ncol(data) )
names(v1) <-  colnames(data)
attr( data, "variable.labels" ) <- v1
attr(data,"variable.labels")["pid"] <- "Person ID"
attr(data,"variable.labels")["height"] <- "Height of a person"
attr(data,"variable.labels")["y"] <- "Gender"

#****
# (3) define some value labels
v1 <- c(10,20)
names(v1) <- c("male", "female" )
attr( data$y, "value.labels" ) <- v1

#****
# (4a) run PSPP to produce a sav file
write.pspp( data, datafile="example_data1",
        pspp.path="C:/Program Files (x86)/PSPP/bin/" )

#****
# (4b) produce strings instead of factors
write.pspp( data, datafile="example_data2",
        pspp.path="C:/Program Files (x86)/PSPP/bin/", as.factors=FALSE )

#****
# write sav file using haven package
library(haven)
haven::write_sav( data, "example_data1a.sav" )

#****
# write sav file using sjlabelled package
library(sjlabelled)
data <- sjlabelled::set_label( data, attr(data, "variable.labels") )
sjlabelled::write_spss( data, "example_data1b.sav" )

## End(Not run)

miceadds documentation built on May 29, 2024, 11:05 a.m.