# SPSS to R: An R package to convert SPSS syntax

**Educate-R - R**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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<span class="p">(</span>devtools<span class="p">)</span>
install_github<span class="p">(</span><span class="s">"SPSStoR"</span><span class="p">,</span> <span class="s">"lebebr01"</span><span class="p">)</span>
```

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<span class="p">(</span>SPSStoR<span class="p">)</span>
file <span class="o"><-</span> paste<span class="p">(</span>system.file<span class="p">(</span><span class="s">'SPSSsyntax'</span><span class="p">,</span> package <span class="o">=</span> <span class="s">'SPSStoR'</span><span class="p">),</span>
<span class="s">"/getDescExamp.txt"</span><span class="p">,</span> sep <span class="o">=</span> <span class="s">''</span><span class="p">)</span>
spss_to_r<span class="p">(</span>file<span class="p">)</span>
```

### Output:

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

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

```
file <span class="o"><-</span> paste<span class="p">(</span>system.file<span class="p">(</span><span class="s">'SPSSsyntax'</span><span class="p">,</span> package <span class="o">=</span> <span class="s">'SPSStoR'</span><span class="p">),</span>
<span class="s">"/ttestOneSampExamp.txt"</span><span class="p">,</span> sep <span class="o">=</span> <span class="s">''</span><span class="p">)</span>
spss_to_r<span class="p">(</span>file<span class="p">)</span>
```

### Output:

```
<span class="p">[</span><span class="m">1</span><span class="p">]</span> <span class="s">"# x is the name of your data frame"</span>
<span class="p">[</span><span class="m">2</span><span class="p">]</span> <span class="s">"with(x, t.test(brake, mu = 322, conf.level = .90)"</span>
```

# 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

**leave a comment**for the author, please follow the link and comment on their blog:

**Educate-R - R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.