Introduction
This article describes how to import an SPSS data set into Displayr using R.
Requirements
- An SPSS (*.sav) data set saved as a URL. In this example, we have a technology data set saved on a website.
- A Displayr document.
Method 1 - foreign R package
1. Select Data Sets > 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"
dataset = suppressWarnings(read.spss(location,
use.value.labels = TRUE,
to.data.frame = TRUE))
Here, we are using the use.value.labels argument to import with value labels displayed.
4. Note this will import using the variable names. Variable labels are not returned so you will need to update these directly in your Data Sets tree.
Method 2 - haven R package
This method additionally supports SPSS's compressed data files (*.zsav).
1. Select Data Sets > 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(haven)
location = "https://wiki.q-researchsoftware.com/images/3/35/Technology_2018.sav"
dataset = read_spss(location)
# Replace values with labels
dataset = as_factor(dataset)
Here we use the as_factor function to replace the values with labels.
4. Note this will import using the variable names. Variable labels are not returned so you will need to update these directly in your Data Sets tree.