data-raw/build_extras.R

library(tidyverse)

#Nokken & Poole scores with bio_id column -- to join us senators to CSUSA data.
#Perhaps include (in some fashion) in package.
# voteview_nokken_poole <- read.csv(url("https://voteview.com/static/data/out/members/HSall_members.csv"),
#   stringsAsFactors = FALSE) %>%
#   select(chamber, congress, bioguide_id, icpsr) %>%
#   unique()

#Previous
sen115 <- jsonlite::fromJSON('/home/jtimm/jt_work/GitHub/packages/uspoliticalextras/data-raw/us-senate-115.json') %>%
  mutate(congress = 115)%>%
  rename(openly_lgbtq = openley_lgbtq)%>%
  select(-govtrack)


house115 <- readxl::read_excel('/home/jtimm/jt_work/GitHub/packages/uspoliticalextras/data-raw/us-house-115.xlsx') %>%
  mutate(congress = 115,
         district=as.character(district),
         at_large = ifelse(at_large == 'yes', TRUE, FALSE),
         vacant = ifelse(vacant == 'yes', TRUE, FALSE),
         twitter_url = gsub('^.*/', '', twitter_url)) %>%
  mutate_at(c('thomas', 'votesmart', 'maplight'), as.character) %>%
  mutate_at(c('date_of_birth', 'entered_office', 'term_end'), as.Date) %>%
  mutate_at(c('latitude', 'longitude'), as.character) %>%
  rename(twitter_handle = twitter_url) %>%
  select(-address)



##State fips
options(tigris_use_cache = TRUE, tigris_class = "sf")
states <-tigris::states(cb = TRUE) %>%
  data.frame() %>%
  select(STATEFP, STUSPS) %>%
  mutate(STATEFP = as.integer(STATEFP))%>%
  rename(state_fips = STATEFP,
         state_abbrev = STUSPS)


#Lawmaker bios via CivilServiceUSA
git <- 'https://raw.githubusercontent.com/CivilServiceUSA/'

uspol_csusa_senate_bios <- jsonlite::fromJSON(url(paste0(git, 'us-senate/master/us-senate/data/us-senate.json'))) %>%
  mutate(congress = 116) %>%
  bind_rows(sen115) %>%
  rename(state_abbrev = state_code,
         bioguide_id = bioguide,
         state=state_name) %>%
  select(-state_name_slug, -name_slug,
         -name_suffix:-pronunciation, -openly_lgbtq,
         -thomas:-title, -middle_name, -facebook_url, -state_code_slug,
         -phone:-contact_page, -twitter_url:-photo_url, -name) %>%

  left_join(voteview_nokken_poole %>%
              filter(congress %in% c(115,116) & chamber == 'Senate'),
            by = c('congress','bioguide_id')) %>%
  left_join(states) %>%
  select(congress:state_fips, state:twitter_handle) %>%
  arrange(congress, state_fips) %>%
  as.tibble()

#Thad Cochran bioguide_id issue -- being updated -- carry on


uspol_csusa_house_bios <- jsonlite::fromJSON(url(paste0(git, 'us-house/master/us-house/data/us-house.json'))) %>%
  mutate(congress = 116) %>%
  rename(state=state_name) %>%
  mutate_at(c('date_of_birth', 'entered_office', 'term_end'), as.Date) %>%
  bind_rows(house115) %>%
  rename(district_code = district,
         state_abbrev = state_code,
         bioguide_id = bioguide) %>%
  select(-state_name_slug, -at_large:-vacant,-name_slug,
         -name_suffix:-pronunciation, -openly_lgbtq,
         -thomas:-title, -middle_name, -facebook_url, -state_code_slug,
         -phone:-contact_page, -twitter_url:-photo_url, -name) %>%
  mutate(district_code = as.integer(district_code),
         district_code = as.integer(replace_na(district_code, 0)))%>%

  left_join(voteview_nokken_poole %>%
              filter(congress %in% c(115,116) & chamber == 'House'),
            by = c('congress','bioguide_id')) %>%
  group_by(first_name, last_name) %>%
  fill(chamber, icpsr, .direction = "up") %>%
  left_join(states) %>%
  select(congress:state_fips, state:twitter_handle) %>%
  arrange(congress, state_fips, district_code) %>%
  as.tibble()





setwd("/home/jtimm/jt_work/GitHub/packages/uspoliticalextras")
#Output
usethis::use_data(uspol_csusa_senate_bios, overwrite=TRUE)
usethis::use_data(uspol_csusa_house_bios, overwrite=TRUE)
#devtools::use_data_raw()
jaytimm/uspoliticalextras documentation built on March 17, 2020, 3:44 a.m.