How to Import SPSS Data Sets into R

In this tutorial, we show you a quick and simple way to import SPSS (.sav) data files into R using the haven package.

We will be working with RStudio, a program that makes it easier to work with R.

 

Quick Steps

  1. Type install.packages(“tidyverse”) and select enter on your keyboard to install tidyverse packages
  2. From the Environment tab, click Import Dataset -> From SPSS…
  3. Click Browse
  4. Browse to the SPSS data file you want to import
  5. Double-click on the SPSS file
  6. Click Import
  7. To review your imported data file type head(your.data) and select enter on your keyboard (recommended).

Install Haven

R doesn’t come with the haven package installed, so you will need to install it if you haven’t already done so. We recommend installing the complete set of tidyverse packages, which includes the haven package.

To install the tidyverse packages, type the following in your RStudio console:

install.packages(“tidyverse”)

Select the Enter key on your keyboard.  You may need to wait for a minute or so for the packages to be installed.

Import Your SPSS File into R

The easiest way to import your SPSS (.sav) data file into R using RStudio is as follows.

Go to the top right pane of RStudio and ensure that the Environment tab is selected.

Ensure Environment tab is selected

Then click Import Dataset and select From SPSS…  from the drop-down menu illustrated below:

Click on Import Dataset and select from SPSS...

 

This brings up the Import Statistical Data window shown below.  Click the Browse button.

Import Statistical Data window.

Browse to the SPSS (.sav) data file that you want to import into R.  Double-click on the file to select it. 

Once you have selected your SPSS data file, your Import Statistical Data window should look something like the one below.  Here we can see:

(1) The file and file path (or URL) of the SPSS data file that you want to import into RStudio.

(2) The Data Preview of this file.

(3) The autogenerated name that your file will have in R.  To change this file name, just place your cursor in the Name field and edit it.

(4) The R code for the import

(5) The Import button.

Import Statistical Data window with SPSS data file

Click the Import button.  You should now seem something similar to the screenshot below in RStudio:

SPSS data set imported into RStudio

In this screenshot, we can see the following:

(1) The first records of our imported data set

(2) Information about the data frame we have imported from SPSS. tbl_dbf tells us that our SPSS data set has been imported as a tibble – a type of data frame.  We can also see that our data frame has five variables and 50 observations.

(3) The R code that was generated when we imported our SPSS file into R.

Review Your Imported Data Set

Once you have imported your SPSS data file, it’s a good idea to review it in RStudio.  A straightforward way to do this is to display the first part of the data set by typing the following:

head (your.data)

Replace your.data with the name of your data frame in R.  The name of our data frame is frisbee, so we type head(frisbee).

Select the Enter key on your keyboard to execute this command in RStudio.  By default, the head() command will display the first six records of your data frame as per the screenshot below:

Displaying first part of tibble data frame in R

You can see that our example data set includes two types of data:

  • dbl: Numeric variables.
  • dbl+lbl: These are variables that are coded numerically but that are associated with a value label.  For example, in our Gender variable, 0 represents a female participant, and 1 represents a male participant.

Note that you can also review your data set by checking the view in the top left window of RStudio.  This view, however, will not tell you the data type of your variables.

Save Your Tibble Data Frame (Optional)

If you want to save your imported data set as a tibble data frame, so that you can work with it in a future R session, you can do this by typing the following:

saveRDS(Rtibble, “savedtibble.rds”)

Replace Rtibble with the name of your tibble data frame in R.   Our tibble data frame is called frisbee.

Replace savedtibble.rds with the file name and file path that you want to use to save your file on your computer.  There are two important things to note here:

  1. If you don’t specify a file path, your tibble data frame will be saved in your “working directory”.
  2. We need to type file paths in R in a particular way.  Specifically, we need to replace the backwards slashes (\) that we normally see in Windows file paths with forward slashes (/) in R.   I want to save my tibble data frame to the file path C:\Users\user\OneDrive\Documents\R\Datasets with the file name frisbee_tibble.rds so in R, this will be C:/Users/user/OneDrive/Documents/R/Datasets/frisbee_tibble.rds.

Putting this all together, the command we type in RStudio for our example is:

saveRDS(frisbee, “C:/Users/user/OneDrive/Documents/R/Datasets/frisbee_tibble.rds”)

Select the Enter key on your keyboard to save the tibble data frame to the specified location on your computer.

***************

That’s it for this tutorial.  You should now be able to import SPSS (.sav) data sets into R using the haven package.

***************