Daniel Oesch (University of Lausanne) has developped a schema of social classes, which he discusses and applies in different publications (see Oesch 2006a, 2006b). On his personnal website, he offers Stata, SPSS, and SAS scripts to generate the class schema with data from different surveys.
Scholars working with other programs (especially R) might be interested in using Oesch’s class schema as well. In this vignette, I show how to apply Oesch’s class schema on data from the European Social Survey (ESS) using R.
Rather than preparing an R script that would generate the class variables directly from the raw ESS data, my strategy is the following:
idno
), the country (cntry
), the survey wave (essround
) and the class variables.Steps 1 to 4 are documented in a GitHub repository, which also contains the final CSV file.
Here, I will focus on the last step and demonstrate how anyone can now download the CSV file and merge it with the ESS raw data in R.
In R, we start by loading the necessary packages for this demonstration (if needed, install them with install.packages
).
# install.packages("dplyr")
# install.packages("essurvey")
# install.packages("magrittr")
# install.packages("RCurl")
library(dplyr) # Used for data wrangling
library(essurvey) # Downloads main ESS datafiles
library(magrittr) # Allows pipe operator
library(RCurl) # Downloads files from the web
We take advantage of the essurvey
package, which allows downloading the ESS integrated files, directly from the ESS website. Save your ESS email as an environment variable with the essurvey::set_email
function (make sure to register your email on the ESS website beforehand).
#set_email("your@email.com")
We import the ESS raw data using the essurvey::import_rounds
function. For the sake of this demonstration, we will only download the eighth round of the ESS, but the demonstration would work with any round.
#set_email("your@email.com")
ess8 <- import_rounds(8)
The CSV file with the class variables is stored on GitHub. We save the URL of the CSV file in an object called url
.
url <- "https://raw.githubusercontent.com/jolyphil/" %>%
paste0("oesch-class/master/data/oesch_class_ess_1-8.csv")
We import the CSV file from the web using RCurl:getURL
and load it as a dataframe called classvar
. This dataset contains observations for all the rounds of the ESS. We need to convert the variables idno
and essround
from integer to numeric to merge the data later.
classvar <- url %>%
getURL() %>%
read.csv(text = .) %>%
mutate(idno = as.numeric(idno), # Convert to numeric to allow merge
essround = as.numeric(essround))
Finally, we merge the ESS raw data (ess8
) and the class variables (classvar
) using the respondent number (idno
), the country (cntry
), and the survey wave (essround
) as keys.
finaldata <- ess8 %>%
left_join(classvar, by = c("idno", "cntry", "essround"))
finaldata
now contains the original variables of the ESS together with Oesch’s class variables.
finaldata %>%
select(idno, cntry, essround, gndr, agea, class5, class8, class16)
## # A tibble: 44,387 x 8
## idno cntry essround gndr agea class5 class8 class16
## <dbl> <chr> <dbl> <dbl+lbl> <dbl+lbl> <int> <int> <int>
## 1 1 AT 8 2 34 1 7 13
## 2 2 AT 8 1 52 3 2 4
## 3 4 AT 8 2 68 5 6 12
## 4 6 AT 8 1 54 5 4 8
## 5 10 AT 8 2 20 4 6 11
## 6 11 AT 8 2 65 3 2 4
## 7 12 AT 8 2 52 3 2 4
## 8 13 AT 8 2 44 2 7 14
## 9 14 AT 8 2 22 4 8 15
## 10 15 AT 8 2 41 5 6 12
## # ... with 44,377 more rows