Learn R Programming

miceadds (version 1.5-0)

write.pspp: 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 envoked via the system command from within R(FALSE).

Value

  • A dataset in sav format (SPSS format).

See Also

See also foreign::write.foreign. For writing sav files see also haven::write_sav and sjmisc::write_spss. For convenient viewing sav files we recommend the freeware program ViewSav, see http://www.asselberghs.dds.nl/stuff.htm.

Examples

Run this code
#############################################################################
# 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 the haven package
library(haven)
haven::write_sav( data , "example_data1a.sav" )      

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

Run the code above in your browser using DataLab