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

Description Usage Arguments Value Author(s) See Also Examples

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

1
2
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).

Author(s)

Alexander Robitzsch

The code is 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 sjmisc::write_spss.

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

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
## 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 sjmisc package
library(sjmisc)
data <- sjmisc::set_label( data, attr(data, "variable.labels") )
sjmisc::write_spss( data , "example_data1b.sav" )

## End(Not run)


Search within the miceadds package
Search all R packages, documentation and source code

Questions? Problems? Suggestions? or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.