Load the Data

load("exit-poll-2016.RData")

Question 1.1: What is the unit of observation?

Answer: The unit of observation is the respondent’s unique ids, and respondents show up twice for both candidate ratings.

Question 1.2: Reshape respondent rows

exit <- exit %>%
  pivot_wider(names_from = favorable.cand,
              values_from = favorable.rating,
              names_prefix = "favorable.")

Question 1.3: Checkrespondent id

nrow(exit) == n_distinct(exit$id)
## [1] TRUE
nrow(exit)
## [1] 2957

Question 1.4: Recode favorability labels

exit <- exit %>%
  mutate(across(c(favorable.clinton, favorable.trump),
                ~ factor(.x,
                         levels = c(1, 2, 9),
                         labels = c("favorable", "unfavorable", "no answer"))))

Question 2: Combine Education Columns

exit <- exit %>%
  mutate(across(starts_with("educ."), ~ na_if(.x, 99)),
         educ = case_when(educ.hs == 1 ~ "hs",
                          educ.somecoll == 1 ~ "some college",
                          educ.bach == 1 ~ "bachelors",
                          educ.postgrad == 1 ~ "postgrad",
                          TRUE ~ NA_character_),
         educ = factor(educ,
                       levels = c("hs", "some college", "bachelors", "postgrad"))) %>%
  select(-starts_with("educ."))

Question 3: Split sex age race

exit <- exit %>%
  separate(sex.age.race, into = c("sex", "age", "race"), sep = " ") %>%
  mutate(sex = na_if(sex, "unknown"),
         age = na_if(age, "-999"),
         race = na_if(race, "NA"))

Question 4: Create Third Party Variable

exit <- exit %>%
  mutate(third.party = case_when(PRSPA16 %in% c(1, 2) ~ 0,
                                 PRSPA16 %in% c(3, 4) ~ 1,
                                 TRUE ~ NA_real_)) %>%
  select(-PRSPA16)

Question 5: Convert married to a Dummy Variable

exit <- exit %>%
  mutate(married = if_else(married == 1, 1, 0))

Question 6: Label PHIL3 and partyid

exit <- exit %>%
  mutate(PHIL3 = factor(PHIL3,
                        levels = 1:3,
                        labels = c("liberal", "moderate", "conservative")),
         partyid = factor(partyid,
                          levels = 1:4,
                          labels = c("democrat", "republican",
                                     "independent", "libertarian")))

Question 7: Column Names

exit <- exit %>%
  rename("Voter ID" = id,
         "Belief System" = PHIL3,
         "Political Party" = partyid,
         "Clinton Favorability" = favorable.clinton,
         "Trump Favorability" = favorable.trump,
         "Age Group" = age,
         "Third Party Vote" = third.party,
         Sex = sex,
         Race = race,
         Education = educ,
         Married = married)