Importing SPSS data in R – R tutorial

38

R has the ability to import and export data in many formats. In case if you are looking to import SPSS data in R, then here is the trick for you. Creating a data frame in R is easy.  The keyboard data entry works well for small datasets. For larger datasets, it is probably good to use the methods of importing data from existing text files, Excel spreadsheets, statistical packages, or database management systems.

There are two functions to import SPSS data in R. One is read.spss() function that is present in a pre-installed package named as foreign package. The other option is to make use of spss.get() function in Hmisc package. For using this function you have to install the Hmisc package. spss.get() function automatically sets many parameters of read.spss(), hence it is easy for importing data.

Example

To install Hmisc package use the command install.packages("Hmisc")

Then use the following code to import the data

library(Hmisc)
dataframe <- spss.get("carsales.sav", use.value.labels=TRUE)

The above code reads carsales.sav SPSS data file, use.value.labels=TRUE tells the function to convert variables with value labels into R factors with those same levels, and dataframe is the resulting R data frame.