How to Read & Write SPSS Files (.sav) in R Statistical Environment (2024)

In data analysis, it is common to encounter data saved in different formats, and being able to read them is crucial. In this blog post, we will focus on reading .sav files in R. These files are created by SPSS, a popular software used for statistical analysis. There are several ways to read .sav files in R, including using the read_sav function from the haven package or the read.spss function from the foreign package. We’ll cover both methods in this post, along with tips on importing data from URLs and converting R dataframes to SPSS files. Additionally, we will explore how to import data from other common formats like CSV and Excel to SPSS files. By the end of this post, you will understand how to read and write SPSS files in R and be ready to tackle any data import challenges you might face in your analysis.

Save

Table of Contents

  • Requirements
  • Data Import in R
  • How to Open an SPSS .sav File in R
    • Importing a .sav file in R using Haven’s read_sav
    • Reading an SPSS file from a URL using R
    • Reading a .sav file in R using Foreign’s read.spss
  • Writing R Dataframes to SPSS Files
    • R from CSV to SPSS files
    • R from Excel to SPSS files
  • Conclusion: Read .sav in R
  • R Tutorials

Requirements

To follow this blog post on importing SPSS files in R, there are a few requirements that you need to fulfill. First, you must have data in .sav format to import into R. Second, you will need a basic knowledge of R programming to execute the code examples provided in this post.

In addition, you will need to have the haven package installed in R to import .sav files. If you prefer to use the foreign package instead, you will need to install that package instead. If you want to write R dataframes to SPSS files, you will need the haven or foreign package to do that as well. Here is how to install the packages in R:

install.packagse(c('haven', 'foreign'))Code language: R (r)

Of course, if you only want to use one of them, remove the one you do not want to install. Note, that it might be good to check your version of R and update R if you are running an old version.

To follow the examples in this post, you must have a working internet connection to download the .sav files from a URL, if applicable. Lastly, is important to note that the steps in this post assume that the SPSS files you are importing into R do not require additional data cleaning or wrangling.

To summarize, with the correct data in .sav format, basic knowledge of R, and the necessary packages installed, you can easily follow this post on importing SPSS files in R.

Save

Data Import in R

R is a great statistical programming environment, and we can store our data in the .rda format. However, sometimes we may collaborate with other researchers using other statistical software and/or storing their data in different formats. Luckily, as an R user, we can import data from a range of different formats; SAS (.7bdat), Stata (.dta), Excel (e.g., .xlsx), and CSV (.csv) to name a few. Here are some tutorials about how to import data from some of these formats:

  • How to Import SAS files in R
  • How to Read Stata (.dta) Files and Write .dta Files in R

How to Read & Write SPSS Files (.sav) in R Statistical Environment (3)

  • Save

Can R read SPSS files?

In this brief section, we will quickly answer the question: Can R read SPSS files? The short answer is yes! Here’s how:
1) library(haven), and then
2) read_sav(PATH_TO_YOUR_FILE)
Read the post for more details!

How to Open an SPSS .sav File in R

In this section, we will go into more detail on how to load a .sav file into an R dataframe. We will start using the R package haven and then continue using the foreign package.

Importing a .sav file in R using Haven’s read_sav

In this section, we will learn how to load an SPSS file in R using the package Haven and the function read_sav.

First, we are going to start by loading the Haven library:

library(haven)Code language: R (r)

In the second step, we will create the dataframe from the SPSS file using the read_sav function. Note that, when we load a file using the Haven package, it is important to remember that it will look for the file in the R script’s working directory. In the read_sav example below, we will use an example SPSS file. It can be downloaded here and should be put in the correct folder (or change the path in the code chunk below):

df <- read_sav("./SimData/survey_1.sav")Code language: R (r)

In the code chunk above, we create the dataframe df, and we can use the R function head() to display the 10th first rows:

head(df, 10)Code language: R (r)

How to Read & Write SPSS Files (.sav) in R Statistical Environment (4)

Reading an SPSS file from a URL using R

In this next read SPSS file in R example, we will read a .sav file from a URL. Here we are going to use this dataset.

df <- read_sav(“http://staff.bath.ac.uk/pssiw/stats2/PsychBike.sav”) head(df)Code language: JavaScript (javascript)

Now that you have imported your file, you may want to use R to reshape the data from wide to long to carry out, e.g., probit regression.

Reading a .sav file in R using Foreign’s read.spss

In this section, we will use read.spss from the foreign package. More specifically, we will read SAV files using read.spss.

df <- read_sav(“http://staff.bath.ac.uk/pssiw/stats2/PsychBike.sav”)head(df)Code language: R (r)

How to Read & Write SPSS Files (.sav) in R Statistical Environment (5)

  • Save

Now, there are some steps we may need to go through before analyzing our data. First, we may want to remove a column (or two columns) in R’s dataframe. Second, we may need to dummy code our categorical variables in R. Finally, we may also want to inspect the data, calculate descriptive statistics, and visualize the data (e.g., with a scatter plot).

Writing R Dataframes to SPSS Files

This section will teach us how to write dataframes to .sav files. We will learn how to load CSV and Excel files into R dataframe objects and then save them as SPSS files.

R from CSV to SPSS files

In this subsection, we are going to read a CSV file in R. After we have read this CSV file we are going to save it as a .sav file.

library(readr) df <- read_csv("./SimData/FirstDayData.csv")Code language: R (r)

Now that we have imported the CSV file into an R dataframe, we can save it as an SPSS file using the write_sav function from the Haven package.

library(haven) write_sav(df, “./SimData/FirstDayData.csv”)Code language: R (r)

How to Read & Write SPSS Files (.sav) in R Statistical Environment (6)

  • Save

R from Excel to SPSS files

In this subsection, we are going to do the exact same as above, except that we load an Excel file in R.

library(readxl)df <- read_excel("./SimData/example_concat.xlsx")Code language: R (r)
  • Learn more about reading xlsx files in R

Now that we have imported the Excel file into an R dataframe. The final thing we will do is to save this dataframe to an SPSS file. Again, we will be using the write_sav function from the Haven package.

library(haven) write_sav(df, "./SimData/example_concat.sav")Code language: R (r)

How to Read & Write SPSS Files (.sav) in R Statistical Environment (7)

  • Save

Conclusion: Read .sav in R

In this post, you learned how to import .sav files into R. We covered using the Haven and Foreign packages to read SPSS files from both local files and URLs and how to write R dataframes to SPSS files. Additionally, we showed you how to import data from CSV and Excel files into SPSS format.

Through these methods, you can easily and efficiently import your SPSS data into R for analysis. Whether you are working with a large or smaller dataset, these packages provide the flexibility and tools to make data import seamless.

Now that you have learned how to read .sav files in R, please share this post with others who may find it helpful. If you have any questions or suggestions, feel free to comment below. Happy analyzing!

R Tutorials

  • How to Check if a File is Empty in R: Practical Examples
  • How to Rename Column (or Columns) in R with dplyr
  • How to Remove/Delete a Row in R – Rows with NA, Conditions, Duplicated

  • Save

How to Read & Write SPSS Files (.sav) in R Statistical Environment (2024)

FAQs

How to read SPSS data into R? ›

Method 1 - foreign R package
  1. Select Data Sources > Plus (+) > R.
  2. Enter a name for the data set under Name.
  3. Paste the below R code where it states "Enter your R code here": library(foreign) location = "https://wiki.q-researchsoftware.com/images/3/35/Technology_2018.sav" ...
  4. Note this will import using the variable names.
Apr 22, 2024

How to run a sav File? ›

How you can open a . sav file without SPSS
  1. Convert the file to a CSV and open in a spreadsheet - you can use our free converter to do this and then open the CSV in Excel, Google Sheets or Numbers.
  2. Use the Open Source PSPP software - this requires you to download and install PSPP and navigate the clunky interface.

How to change sav to CSV? ›

Convert and analyze your SAV file

Start analyzing your SAV file directly in AddMaple with automated cross-tabs, summarized charts, word clouds, histograms and more. Simply connect your file and it will be converted ready for you to download as a CSV.

Is SPSS compatible with R? ›

You can also run IBM SPSS Statistics from an external R process, such as an R IDE or the R interpreter. In this mode, you still have access to all of the functions in the R Integration Package for IBM SPSS Statistics, but you can develop and test your R programs with the R development environment of your choice.

How to read SPSS files? ›

In any version of SPSS, you can open a text or CSV file by using File > Open > Data. Prior to SPSS version 25, you could alternatively use File > Read Text Data to begin importing a text or CSV file; this functioned identically to File > Open Data.

What can read .sav files? ›

SAV files:
  • IBM SPSS Statistics: This is the native software for opening and working with . ...
  • PSPP: This is a free and open-source alternative to IBM SPSS Statistics.
  • RStudio: This is a free and open-source software environment for statistical computing and graphics.
  • Excel: Excel can open .
Aug 28, 2015

Can you open a sav file in R? ›

The code below can be used to import SPSS data files (. sav) into R. Additional code is provided to assist data users with converting date variables which get imported into R as number of seconds since the start of the Gregorian calendar. The code will reformat the date from number of seconds to yyyy-mm-dd.

Can I open a .sav file in Excel? ›

The Colectica for Excel addin for Microsoft Excel allows you to open SPSS (. sav) data files in Microsoft Excel. The addin imports and converts all data, as well as metadata including variable and value labels.

Can you edit .sav files? ›

With Save File Editor, you can now directly edit all . SAV files that you have created during gameplay.

How do I save an SPSS output as SAV file? ›

SPSS: Saving a Data File
  1. Select File in the Data Window (not File in the Viewer Window)
  2. Select Save As.
  3. Select Variables for modification, if you would like to choose which variables that you would like to save within your project.
Jul 19, 2023

How to turn SPSS file into CSV? ›

Exporting to CSV format
  1. Open an existing SPSS Statistics dataset and make it the active tab (click anywhere in the tab to make it active).
  2. From the menus choose: File > Export data > CSV data... ...
  3. Click Export after specifying the CSV export parameters.

How to interpret the output of SPSS? ›

Doing the T-Test Procedure in SPSS

To interpret the t-test results, all you need to find on the output is the p-value for the test. To do an hypothesis test at a specific alpha (significance) level, just compare the p-value on the output (labeled as a “Sig.” value on the SPSS output) to the chosen alpha level.

How do you read a dataset in R? ›

Reading R Data Files

When R calls load(), all of the R objects saved in the file are loaded into R. The names given to these objects when they were originally saved will be given to them when they are loaded. The command > ls() can be used to print out all of the objects currently loaded into R.

How do you find the R value and P value in SPSS? ›

Obtaining Pearson r and Spearman rho

Click on Analyze\Correlate\Bivariate. Select your two variables and move them into the box Variables. In the Correlation Coefficients section, Pearson is the default option. If you wish to request the Spearman rho, tick the Spearman box as well (or instead).

How to see variable data in R? ›

To see all the variable names, use names() , and to see all the variable types, use spec() and str() . The spec() function shows you the variable specifications. The str() function gives the same information, plus a listing of the first few values for each variable.

Top Articles
Latest Posts
Article information

Author: Arline Emard IV

Last Updated:

Views: 6258

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Arline Emard IV

Birthday: 1996-07-10

Address: 8912 Hintz Shore, West Louie, AZ 69363-0747

Phone: +13454700762376

Job: Administration Technician

Hobby: Paintball, Horseback riding, Cycling, Running, Macrame, Playing musical instruments, Soapmaking

Introduction: My name is Arline Emard IV, I am a cheerful, gorgeous, colorful, joyous, excited, super, inquisitive person who loves writing and wants to share my knowledge and understanding with you.