Chapter 2 Getting Ready

This short setup chapter ensures everyone can run the examples on their own laptop before class.

2.1 Install R and RStudio

2.2 Folder structure

Place the course folder anywhere convenient. The book assumes the working directory is the course root (where ess.csv lives). To set it inside RStudio: Session > Set Working Directory > To Source File Location.

# check current working directory
getwd()
# list course files
list.files()

2.3 Load the ESS data once

We use a pre-cleaned CSV with 80k+ respondents from GB, DE, and FR. The code below keeps only the variables used in the book and handles common missing codes (““, 7x, 8x, 9x often mean non-response in ESS).

library(dplyr)
library(readr)

ess_raw <- read_csv("ess.csv", show_col_types = FALSE)

ess <- ess_raw |>
  filter(cntry %in% c("GB", "DE", "FR")) |>
  mutate(across(where(is.character), ~ na_if(.x, ""))) |>
  mutate(
    cntry = factor(cntry),
    across(c(agea, gndr, ppltrst, netustm, nwsptot), ~ suppressWarnings(as.numeric(.x)))
  )

# quick glimpse
ess |> select(cntry, agea, gndr, ppltrst, netustm, nwsptot) |> slice_head(n = 5)

Tip: keep ess in your environment while you work across chapters to avoid reloading.