The basic functions in R cannot import SPSS files. This means that you now need an additional library to have a function that can import the SPSS file. There are several different libraries available for this purpose, but here we will focus on the foreign
library.
When you need an additional library, you must first install it once. This can be done using the install.packages()
function, where you provide the name of the library as an argument - in our case, foreign
.
install.packages("foreign")
Now you have installed the additional functions from the foreign
package. To be able to use them, you need to load them briefly every time you want to use them in an R session. This is done with the library()
function, using the package name as the argument.
library("foreign")
Now we will load the exam
dataset in R, but this time in SPSS format. The special thing about SPSS is that it often contains descriptions of code values or variable descriptions. However, we do not use these in R; instead, we use the codebook of a dataset for that. You will learn how to work with the codebook in the later examples, but for this simple training dataset, we will skip that for now.
From the foreign
library, we use the read.spss()
function, where we also use the file.path()
function again to be able to use the path
object. There are two additional arguments in the read.spss()
function that are new: use.value.labels
and to.data.frame
. The latter simply indicates that the final object should be a dataset and not a matrix. With the argument use.value.labels = FALSE
, we turn off the inclusion of value labels. This way, we avoid issues during import because in SPSS, often only the endpoints of a scale are labeled, and we would end up with mixed variables that would all be read as character even though they are numeric.
The import process is quite simple:
examspss <- read.spss(
"./data/exam.sav",
use.value.labels = FALSE,
to.data.frame = TRUE
)
That’s it for now on data import!