Skip to main content

Section 2.2 Importing the Data Using the Haven Package

The haven package is a very useful tool within the R environment because it allows users to import data that has different file extensions created in popular statistical software packages such as SAS, SPSS, and Stata. The haven package enables users to import and export datasets effortlessly by bridging the gap between different data formats. As I mentioned in the first chapter, installing and loading the haven package is the first step.
install.packages('haven')

library(haven)
library(haven) loads the haven package in R, providing access to functions like read_sav() and write_sav(). Now, we are ready to read the 2012 GSS data that will be manipulated for the purpose of practice. You will be able to download this file from the shared Google Drive folder containing the 2012 GSS data. The downloaded 2012 GSS data is an SPSS data file, and SPSS data files are typically based on the .sav extension. I will read the data from the file located on the following path: C:/Users/75JCHOI/OneDrive - West Chester University of PA/WCU Research/R/data/GSS.2012.sav. You will have to use your own file location to read the data. Please watch the following video to learn how to set up a working directory in R.
I will assign the read data to a new data frame labeled GSS.2012. Please note that we could read this SPSS file because we used the haven package, which allows users to import data from different software programs.
GSS.2012 <- read_sav("C:/Users/75JCHOI/OneDrive - West Chester University of PA/WCU Research/R/data/GSS.2012.sav")