SPSS to R: An R package to convert SPSS syntax

November 26, 2013
By

(This article was first published on Educate-R - R, and kindly contributed to R-bloggers)

My first statistical software package I used as an undergrad was SPSS. I was fortunate during my senior year at Luther College to be initially introduced to R. I did not realize it at the time (except for the pretty graphics) that this was the start of something big for me. Fast forward a year to graduate school at the University of Minnesota and the majority of my program was again using SPSS. Under the tutelage of Andy in my first graduate statistics course I started to do analyses in both R and SPSS. After that I started to do all my analyses in R. Fast forward five years this time to my first full-time job after graduate school at Saint Paul Public Schools and again everyone was using SPSS. Fortunately in my year there I was able to significantly push them toward using R more and more.

This long introduction motivates the following package I started writing mostly just to play with regular expressions, but kept playing and came up with a package with many routines. The package I came up with is called SPSStoR which converts SPSS syntax into R syntax. Using regular expressions to parse the SPSS syntax to figure out which routine is being run then prints out the R syntax to do the same analysis done by the SPSS syntax.

As you can imagine, the SPSS language has many routines and this is not all encompassing, however it provides many basic commands such as: * Aggregate * Correlations * Crosstab * Sort Cases * Descriptives * One sample t-test * Independent sample t-test * Get for sav files (SPSS data files) * Master SPSStoR function * Graphics * Frequencies

Examples


You can install the package directly from my github page using the devtools library:

library(devtools)
install_github("SPSStoR", "lebebr01")

Once you install the package, I've included some example SPSS syntax within the package to use as examples. One example syntax file reads in an SPSS data file, sorts the file, then computes descriptive statistics on a handful of variables. The following should run the command from your computer:

library(SPSStoR)
file <- paste(system.file('SPSSsyntax', package = 'SPSStoR'), 
              "/getDescExamp.txt", sep = '')
spss_to_r(file)

Output:

[1] "# x is the name of your data frame"
[2] "library(foreign)"
[3] "x <- read.spss('/data/hubtemp.sav', to.data.frame = TRUE)"
[4] "x <- x[order(DIVISION, STORE, -AGE), ]"
[5] "library(SPSStoR)"
[6] "with(x, descmat(x = list(longmon, tollmon, equipmon, cardmon, wiremon), mean, sd, min, max))"

Here is another example doing a one-sample t-test:

file <- paste(system.file('SPSSsyntax', package = 'SPSStoR'), 
              "/ttestOneSampExamp.txt", sep = '')
spss_to_r(file)

Output:

[1] "# x is the name of your data frame"
[2] "with(x, t.test(brake, mu = 322, conf.level = .90)"

Summary


Although some working R knowledge is needed to run this package, such as knowledge of installing packages and what an object is, this package may be useful to those trying to figure out a similar command in R. Over time I hope to slowly add support for more SPSS routines, see my github page for those that I am currently prioritizing. Let me know via the issues on github if you'd like support for a specific routine or run into problems.

The package home can be found on github: SPSStoR

To leave a comment for the author, please follow the link and comment on his blog: Educate-R - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.