Step 1: clean the data

This notebook reads the raw survey file, cleans it, and saves a clean file. It never changes the raw file. Every decision about who is in the sample is made here, and only here.

# packages
library(tidyverse)
library(here)

# read the raw data: a sample of the 2020 Cooperative Election Study
raw = read_csv(here("data", "raw", "cces_2020_sample.csv"))

The raw file has 5000 respondents. One row is one person.

Party

The survey asks about party on a 7-point scale, from “Strong Democrat” to “Strong Republican”. I collapse it to three groups. People who “lean” toward a party count as members of that party. People who said “Not sure” get a missing value.

clean = raw |>
  mutate(party = case_when(
    str_detect(pid7, "Democrat") ~ "Democrat",
    str_detect(pid7, "Republican") ~ "Republican",
    pid7 == "Independent" ~ "Independent"
  ))

# check the recode: every pid7 answer should land in the right group
clean |> count(pid7, party)
# A tibble: 8 × 3
  pid7                       party           n
  <chr>                      <chr>       <int>
1 Independent                Independent   733
2 Lean Democrat              Democrat      530
3 Lean Republican            Republican    435
4 Not sure                   <NA>          181
5 Not very strong Democrat   Democrat      564
6 Not very strong Republican Republican    414
7 Strong Democrat            Democrat     1320
8 Strong Republican          Republican    823

Support for the border wall

The question: “Increase spending on border security by $25 billion, including building a wall between the U.S. and Mexico.” I turn “Support” / “Oppose” into a 1 / 0 dummy, so that its average is the share who support.

clean = clean |>
  mutate(support_wall = if_else(wall == "Support", 1, 0))

# check the recode
clean |> count(wall, support_wall)
# A tibble: 3 × 3
  wall    support_wall     n
  <chr>          <dbl> <int>
1 Oppose             0  3051
2 Support            1  1945
3 <NA>              NA     4

Missing values

I drop respondents with no party or no answer on the wall question.

# how many are missing each one?
clean |>
  summarize(missing_party = sum(is.na(party)),
            missing_wall = sum(is.na(support_wall)))
# A tibble: 1 × 2
  missing_party missing_wall
          <int>        <int>
1           181            4
clean = clean |>
  drop_na(party, support_wall)

Exclusions

Any rule about who is left out of the study goes here, with a sentence that says why.

# none yet

Save the clean data

clean = clean |>
  select(state, sex, age, educ, race, party, support_wall)

write_csv(clean, here("data", "clean", "clean_data.csv"))

I started with 5000 respondents and ended with 4815. That is 185 dropped.