6.2 C
London
Thursday, December 19, 2024
HomeRImport & Export Data in RHow to Import SPSS Files into R (Step-by-Step)

How to Import SPSS Files into R (Step-by-Step)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

The easiest way to import SPSS files into R is to use the read_sav() function from the haven library.

This function uses the following basic syntax:

data 'C:/Users/User_Name/file_name.sav')

The following step-by-step example shows how to import a SPSS file into R in practice.

Step 1: Download a SPSS File

For this example, we’ll download the SPSS file called healthdata.sav from this page.

Step 2: Install haven Package

Next, we’ll install the haven package in R:

install.packages('haven')

We’ll then load the package:

library(haven)

Step 3: Import the SPSS File

Next, we’ll use the read_sav() function to import the SPSS file:

data C:/Users/bob/Downloads/healthdata.sav')

Once we’ve imported the SPSS file, we can get a quick summary of the data:

#view class of data
class(data)

[1] "tbl_df"     "tbl"        "data.frame"

#display dimensions of data frame
dim(data)

[1] 185   3

#view first six rows of data
head(data)

                 CD EXERC HEALTH
1 1 [ordered]           3      6
2 2 [did not order]     3      7
3 2 [did not order]     5      6
4 2 [did not order]     5      3
5 1 [ordered]           5      6
6 2 [did not order]     2      3

We can see that the file imported successfully as a data frame and that it has 185 rows and 3 columns.

Additional Resources

The following tutorials explain how to import other file types into R:

How to Import CSV Files into R
How to Import Excel Files into R
How to Import SAS Files into R
How to Import .dta Files into R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories