Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export a dataset to SPSS?

Tags:

r

spss

I want to export a dataset in the MASS package to SPSS for further investigation. I'm looking for the EuStockMarkets data set in the package.

As described in http://www.statmethods.net/input/exportingdata.html, I did:

library(foreign)
write.foreign(EuStockMarkets, "c:/mydata.txt", "c:/mydata.sps",   package="SPSS")

I got a text file but the sps file is not a valid SPSS file. I'm really looking for a way to export the dataset to something that a SPSS can open.

like image 328
SmallChess Avatar asked Dec 14 '22 20:12

SmallChess


2 Answers

As Thomas has mentioned in the comments, write.foreign doesn't generate native SPSS datafiles (.sav). What it does generate is the data in a comma delimited format (the .txt file) and a basic syntax file for reading that data into SPSS (the .sps file). The EuStockMarkets data object class is multivariate time series (mts) so when it's exported the metadata is lost and the resulting .sps file, lacking variable names, throws an error when you try to run it in SPSS. To get around this you can export it as a data frame instead:

write.foreign(as.data.frame(EuStockMarkets), "c:/mydata.txt", "c:/mydata.sps", package="SPSS")

Now you just need to open mydata.sps as a syntax file (NOT as a datafile) in SPSS and run it to read in the datafile.

like image 59
Ritchie Sacramento Avatar answered Jan 07 '23 03:01

Ritchie Sacramento


Rather than exporting it, use the STATS GET R extension command. It will take a specified data frame from an R workspace/dataset and convert it into a Statistics dataset. You need the R Essentials for Statistics and the extension command, which are available via the SPSS Community site (www.ibm.com/developerworks/spssdevcentral)

like image 25
JKP Avatar answered Jan 07 '23 05:01

JKP