load("exit-poll-2016.RData")
Answer: The unit of observation is the respondent’s unique ids, and respondents show up twice for both candidate ratings.
exit <- exit %>%
pivot_wider(names_from = favorable.cand,
values_from = favorable.rating,
names_prefix = "favorable.")
nrow(exit) == n_distinct(exit$id)
## [1] TRUE
nrow(exit)
## [1] 2957
exit <- exit %>%
mutate(across(c(favorable.clinton, favorable.trump),
~ factor(.x,
levels = c(1, 2, 9),
labels = c("favorable", "unfavorable", "no answer"))))
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."))
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"))
exit <- exit %>%
mutate(third.party = case_when(PRSPA16 %in% c(1, 2) ~ 0,
PRSPA16 %in% c(3, 4) ~ 1,
TRUE ~ NA_real_)) %>%
select(-PRSPA16)
exit <- exit %>%
mutate(married = if_else(married == 1, 1, 0))
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")))
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)