US Senators Up for Reelection in 2016

07. August 2016 data, programming, r 0
US Senators Up for Reelection in 2016

In my state, I know who my senators are and their reelection status, but I didn’t know any of the senators from other states who were also up for reelection.  Feeling a bit uninformed, I used R to extract information from Wikipedia and summarize the data for me. Not shown in this post is the actual data frame created from the extraction.  That said, the R script is posted below for replication.

After extracting and summarizing the data, I found that there are 34 senators for reelection in 2016.  There are 10 senators from the Democratic party and 24 from the Republican party.  When looking at these particular senators, I thought it would also be good to see some additional information on age and years of service.

Below are the data tables and the R Script I used to do this high-level analysis.

Average age and years in office for Senators up for reelection in 2016
Party Avg Age Avg Years In Office Number of Senators
1 Democratic 67 20 10
2 Republican 61 10 24

 

Senators Up for Reelection in 2016
State Name Party Assumed Office Age Years In Office Seat Up
1 Alabama Shelby, Richard Republican 1987-01-03 82 29 2016
2 Alaska Murkowski, Lisa Republican 2002-12-20 59 13 2016
3 Arizona McCain, John Republican 1987-01-03 79 29 2016
4 Arkansas Boozman, John Republican 2011-01-03 65 5 2016
5 California Boxer, Barbara Democratic 1993-01-03 75 23 2016
6 Colorado Bennet, Michael Democratic 2009-01-21 51 7 2016
7 Connecticut Blumenthal, Richard Democratic 2011-01-05 70 5 2016
8 Florida Rubio, Marco Republican 2011-01-03 45 5 2016
9 Georgia Isakson, Johnny Republican 2005-01-03 71 11 2016
10 Hawaii Schatz, Brian Democratic 2012-12-26 43 3 2016
11 Idaho Crapo, Mike Republican 1999-01-03 65 17 2016
12 Illinois Kirk, Mark Republican 2010-11-29 56 5 2016
13 Indiana Coats, Dan Republican 2011-01-03 73 5 2016
14 Iowa Grassley, Chuck Republican 1981-01-03 82 35 2016
15 Kansas Moran, Jerry Republican 2011-01-03 62 5 2016
16 Kentucky Paul, Rand Republican 2011-01-03 53 5 2016
17 Louisiana Vitter, David Republican 2005-01-03 55 11 2016
18 Maryland Mikulski, Barbara Democratic 1987-01-03 80 29 2016
19 Missouri Blunt, Roy Republican 2011-01-03 66 5 2016
20 Nevada Reid, Harry Democratic 1987-01-03 76 29 2016
21 New Hampshire Ayotte, Kelly Republican 2011-01-03 48 5 2016
22 New York Schumer, Chuck Democratic 1999-01-03 65 17 2016
23 North Carolina Burr, Richard Republican 2005-01-03 60 11 2016
24 North Dakota Hoeven, John Republican 2011-01-03 59 5 2016
25 Ohio Portman, Rob Republican 2011-01-03 60 5 2016
26 Oklahoma Lankford, James Republican 2015-01-03 48 1 2016
27 Oregon Wyden, Ron Democratic 1996-02-06 67 20 2016
28 Pennsylvania Toomey, Pat Republican 2011-01-03 54 5 2016
29 South Carolina Scott, Tim Republican 2013-01-02 50 3 2016
30 South Dakota Thune, John Republican 2005-01-03 55 11 2016
31 Utah Lee, Mike Republican 2011-01-03 45 5 2016
32 Vermont Leahy, Patrick Democratic 1975-01-03 76 41 2016
33 Washington Murray, Patty Democratic 1993-01-03 65 23 2016
34 Wisconsin Johnson, Ron Republican 2011-01-03 61 5 2016

Full R Script:

library(rvest)
library(stringr)
library(lubridate)
library(dplyr)

url <-
  "https://en.wikipedia.org/wiki/List_of_current_United_States_Senators"
senators <- url %>%
  read_html() %>%
  html_nodes(xpath = '//*[@id="mw-content-text"]/table[6]') %>%
  html_table()
senators <- as.data.frame((senators))

# Sanders had a footnote ("[1]") by his party name, so I extracted the text up to the footnote for all senators
senators$Party = str_extract(senators$Party, "([A-Z][a-z]+)")

# Remove unnecessary columns from dataset
senators = senators[, c(-1,-4)]

# Change the class to Factor
senators$State = as.factor(senators$State)
senators$Party = as.factor(senators$Party)
senators$Class = as.factor(senators$Class)
senators$Seat.Up = as.factor(senators$Seat.Up)

# Convert class to date
senators$Assumed.Office = mdy(senators$Assumed.Office)

# Extract name using regex.  Names prior to this looked like: "Lastname, FirstnameFirstName Lastname"
senators$Name = str_extract(senators$Name, "(\\w+,\\s+)([A-Z][a-z]+)")

# Extract birth date from column using regex
senators$Born = ymd(str_extract(senators$Born, "\\d+-\\d+-\\d+"))

# Calculate the number of years in office for each senator
senators$YearsInOffice = trunc(((today() - senators$Assumed.Office) / 365))
senators$YearsInOffice = as.numeric(senators$YearsInOffice)

# Calculate the age of each senator
senators$Age = trunc(((today() - senators$Born) / 365))
senators$Age = as.numeric(senators$Age)

# Calculate the senator's age when they were elected into office
senators$AgeWhenAssumedOffice = trunc(((senators$Assumed.Office - senators$Born) / 365))
senators$AgeWhenAssumedOffice = as.numeric(senators$AgeWhenAssumedOffice)

# Summarize the data ------------------------------------------------------
# What senators are up for election in 2016
seatUp =senators %>% select(State, Name, Party, Assumed.Office, Age, YearsInOffice, Seat.Up) %>% filter(Seat.Up == "2016") %>% arrange(State, Name)

# How many senators have seats that are up for reelection in 2016 by Party
seatUpParty = senators %>% select(Party, Seat.Up) %>% filter(Seat.Up == "2016") %>%  group_by(Party) %>% summarise(n = n())

seatUpState = senators %>% select(State, Seat.Up) %>% filter(Seat.Up == "2016") %>% group_by(State) %>% summarise(n = n())

avgAgeServiceParty = senators %>%
  select(Party, Age, YearsInOffice, Seat.Up) %>%
  filter(Seat.Up == "2016") %>% 
  group_by(Party) %>%
  summarise(
  AvgAge = round(mean(Age), digits = 0),
  AvgYearsInOffice = round(mean(YearsInOffice), digits = 0),
  n = n())

 


Leave a Reply