Read SPSS sav File into R (2 Examples)

 

In this post you’ll learn how to import an SPSS .sav file in R.

The page consists of two examples for the importing of .sav files. To be more specific, the article will contain this:

Let’s just jump right in…

 

Example Data & Packages

Let’s first create some example data in R:

data <- data.frame(x1 = 2:6,                          # Create example data
                   x2 = 7,
                   x3 = letters[4:8],
                   x4 = "x")
data                                                  # Print example data

 

table 1 data frame read spss sav file

 

As you can see based on Table 1, the example data is a data frame and contains five rows and four columns.

In this tutorial, we’ll also need to install and load the haven package:

install.packages("haven")                             # Install haven package
library("haven")                                      # Load haven package

Next, we can export our example data as .sav file to our current working directory:

write_sav(data, "data.sav")                           # Export example data as .sav

Have a look at your working directory on your computer – It should contain an SPSS .sav file called data.sav.

 

Example 1: Import SPSS .sav File into R Using read_sav() Function of haven Package

The following R code explains how to read an SPSS .sav file to R using the read_sav function of the haven package.

Note that we have loaded the haven package already, when we created our example data. However, in case you have not installed and loaded the package yet, you need to do it at this point.

As next step, we can apply the read_sav function of the haven package to import our data into R:

data1 <- read_sav("data.sav")                         # Reading data
data1                                                 # Print imported data
# # A tibble: 5 x 4
#      x1    x2 x3    x4   
#   <dbl> <dbl> <chr> <chr>
# 1     2     7 d     x    
# 2     3     7 e     x    
# 3     4     7 f     x    
# 4     5     7 g     x    
# 5     6     7 h     x

The previous output of the RStudio console shows that we have created a new data set called data1 that contains our example data frame. Note that the read_sav function imports data in the tibble format.

 

Example 2: Import SPSS .sav File into R Using read.spss() Function of foreign Package

An alternative to the haven package (as explained in Example 1) is provided by the foreign package.

We first need to install and load the foreign package to R:

install.packages("foreign")                           # Install & load foreign
library("foreign")

Now, we can apply the read.spss function to load our example .sav file as shown below:

data2 <- read.spss("data.sav", to.data.frame = TRUE)  # Reading data
data2                                                 # Print imported data

 

table 2 data frame read spss sav file

 

Table 2 visualizes the output of the previous code: A data frame containing the values of our example SPSS file.

 

Video & Further Resources

I have recently published a video on my YouTube channel, which explains the R syntax of this article. You can find the video below.

 

The YouTube video will be added soon.

 

In addition, you may want to read some of the related articles on my homepage. You can find a selection of interesting articles below.

 

In summary: In this tutorial, I have explained how to read .sav files in R. Don’t hesitate to let me know in the comments section, if you have further questions. Furthermore, please subscribe to my email newsletter to receive updates on the newest articles.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top