Skip to content

Instantly share code, notes, and snippets.

@johnburnmurdoch
Created May 11, 2021 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnburnmurdoch/5197c86a4a970acf8217d10b8bb0a4f7 to your computer and use it in GitHub Desktop.
Save johnburnmurdoch/5197c86a4a970acf8217d10b8bb0a4f7 to your computer and use it in GitHub Desktop.
install.packages("needs")
library(needs)
needs(sjlabelled, tidyverse, haven, magrittr, ggrepel, sf)
# Load in data from all waves
USoc_indresp_1 <- read_dta("/path to wave 1/", encoding = "latin1")
USoc_indresp_2 <- read_dta("/path to wave 2/", encoding = "latin1")
etc
# Join all waves together, keeping the most recent driving licence record for each respondent, and their vote in the last election
USoc_indresp_10 %>%
dplyr::select(pidp, j_drive, j_vote8, j_indinub_xw) %>%
left_join(USoc_indresp_1 %>% dplyr::select(pidp, a_drive)) %>%
left_join(USoc_indresp_2 %>% dplyr::select(pidp, b_drive)) %>%
left_join(USoc_indresp_3 %>% dplyr::select(pidp, c_drive)) %>%
left_join(USoc_indresp_4 %>% dplyr::select(pidp, d_drive)) %>%
left_join(USoc_indresp_5 %>% dplyr::select(pidp, e_drive)) %>%
left_join(USoc_indresp_6 %>% dplyr::select(pidp, f_drive)) %>%
left_join(USoc_indresp_7 %>% dplyr::select(pidp, g_drive)) %>%
left_join(USoc_indresp_8 %>% dplyr::select(pidp, h_drive)) %>%
left_join(USoc_indresp_9 %>% dplyr::select(pidp, i_drive)) %>%
mutate(j_vote8 = as_character(j_vote8)) %>%
gather(wave, drive, c(2,5:ncol(.))) %>%
group_by(pidp, j_indinub_xw, j_vote8) %>%
summarise(drive = ifelse(1 %in% drive, 1, 2)) %>%
ungroup() %>%
# Weighted counts of vote by driving licence status:
count(drive, j_vote8, wt = j_indinub_xw) %>%
filter(drive > 0) %>%
mutate(drive = c("Yes", "No")[drive]) %>%
# Get rid of records with no valid answer to the voting question:
filter(!j_vote8 %in% c("inapplicable", "refusal")) %>%
group_by(drive) %>%
# Calculate the voting patterns of licence-holders and non-licence-holders:
mutate(share = n/sum(n)*100) %>%
View
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment