Marium Tapal, Irene Ryan, Rana Gahwagy, and Benjamin S. Baumer

The fec16 package provides tamed relational datasets of the Federal Election Commission (FEC)’s 2015-2016 election cycle. Some datasets are included in full, while a sample of the others is available with the option of retrieving the entire datasets through the functions built in the package. Details are given below.


We wanted to create a data package that is easy to use for people just beginning to learn R, for example, in introductory statistics and data science classes. For this purpose, this package is nice because students and instructors do not need to worry about the unnecessary data wrangling and can immediately use the data for analysis.

Our package is inspired by Hadley Wickham’s nycflights13 package. We used the data taming principles from Albert Kim’s fivethirtyeight package, which are explained in detail here.

Basics of tame data

We used the tame data principles to produce uniform data frames that are easy to navigate and link together. The following are the guidelines we used to tame our data:

  1. Variable names:
    • Lower case and with underscores instead of spaces using clean_names()
    • 20 characters or less
    • The same names were used for the same variables in different data frames
    • Names to be meaningful
  2. Variable types:
    • Encode variable with dates with as.Date, unless it is only a year then it would be numeric type
    • Encode categorical variables that a have a limited number of values with as.factor otherwise it would be of character type
  3. Tidy or long data format instead of wide
  4. Missing data has value NA
  5. Removed signs such as % and $ attached to numeric values

Who should use this package?

Anyone interested in US politics and elections who wants to use actual data to think critically and make inferences. We made this package particularly with students and instructors in mind as there is demand for relational data in teaching. fec16 is a one-stop shop for acquiring data of this kind.

Other notes

Datasets Included

Full Datasets

Sample Datasets (with 1000 random rows each)

Functions Included

The following functions retrieve the entire datasets for the sampled ones listed above. The size of the raw file that is downloaded by calling each function is given for reference. All functions have an argument n_max which defaults to the entire dataset but the user can specify the max length of the dataset to be loaded via this argument.

for example:

# The entire expenditures dataset can be accessed by:
all_expenditures <- fec16::read_all_expenditures()

# The first 30 entries in this dataset can be accessed by:
expenditures_30 <- fec16::read_all_expenditures(n_max = 30)

More details can be found on the documentation pages which can be called via: ?function_name

What does the data look like?

The first six rows of the results_house dataset look like:

#> # A tibble: 6 x 13
#>   state district_id cand_id incumbent party primary_votes primary_percent
#>   <chr> <chr>       <chr>   <lgl>     <chr>         <dbl>           <dbl>
#> 1 AL    01          H4AL01… TRUE      R             71310          0.601 
#> 2 AL    01          H6AL01… FALSE     R             47319          0.399 
#> 3 AL    02          H0AL02… TRUE      R             78689          0.664 
#> 4 AL    02          H6AL02… FALSE     R             33015          0.278 
#> 5 AL    02          H6AL02… FALSE     R              6856          0.0578
#> 6 AL    02          H6AL02… FALSE     D                NA         NA     
#> # … with 6 more variables: runoff_votes <dbl>, runoff_percent <dbl>,
#> #   general_votes <dbl>, general_percent <dbl>, won <lgl>, footnotes <chr>

What can we do with this data?

We can use this package to address the (non-exhaustive) list of questions:

  1. Which presidential candidate won majority in more states?
  2. What is the relationship between contributions of candidates and total votes they get?
  3. Which candidate got the most popular vote and how many?

To answer our questions we can make use of some data wrangling and data visualization techniques. Some examples (addressing the questions above) are shown below:

Example 1:

Which presidential candidate won a majority in more states, and how many votes did they get?

Using the results_president dataset, we can also see what actually happened in the 2016 elections.

Here is how we can summarize the number of wins by candidate:

Compare these results to those reported by the Wikipedia. We can show the results using a simple bar chart:

Each win is for a single state. There are 51 total wins for the 50 States and Washington, D.C.

We can see that Hillary Clinton earned the most votes.

Example 2:

What is the relationship between contributions of candidates in the house and total votes they get?

Here we investigate what kind of relationship, if any, the candidates with over a 1000 votes got with the total contributions they made. We are interested in candidates with an ID number so we can join them with the contribution data set by using inner_join.

#> # A tibble: 6 x 5
#> # Groups:   cand_id, cand_name [6]
#>   cand_id   cand_name                cand_pty_affiliation sum_votes contribution
#>   <chr>     <chr>                    <chr>                    <dbl>        <dbl>
#> 1 H0AL02087 ROBY, MARTHA             REP                   63801078      1105157
#> 2 H0AL05163 BROOKS, MO               REP                   30641403       251855
#> 3 H0AL07086 SEWELL, TERRYCINA ANDREA DEM                  169016210      1249038
#> 4 H0AR01083 CRAWFORD, ERIC ALAN RICK REP                   45047170       385616
#> 5 H0AR03055 WOMACK, STEVE            REP                   73193704       568116
#> 6 H0AZ01259 GOSAR, PAUL DR.          REP                   78139008       680874

Next, we plot the contributions and votes on a scatter plot and plot a trend line that would make it easy for us to see the relationship. Since there are outliers in the data, it is best to not use a continuous axis scale in order to see all of the points.

As we can see, the highest contributors got the highest amount of votes so it has a positive correlation, but then it falls after a large contribution amount.

Example 3:

What is the distribution of number of votes across winners in the US House of Representatives?

Visualize the results of the popular vote in the elections and see how many people voted: