Choose one of the New York Times APIs, construct an interface in R to read in the JSON data, and transform it to an R dataframe.
Loading required packages
library(RJSONIO)
We are bringing in the data by calling the specific API for The Campaign Finance hosted by The New York Times developer network
Using the R package to read and parse JSON format files.
# The source url is a combination of the base uri + the site key obtained from the provider for calling
# this sepcific API.
# We are using the variables for year and the name of the candidate. In this case, it is 2008 for Obama.
url <- "http://api.nytimes.com/svc/elections/us/v3/finances/2008/president/candidates/obama.json?
api-key=d567ecd7d91346b9a698db4e245dac5b:9:73349057"
# reading the json in to a dataframe
df <- fromJSON(url, simplifyDataFrame = TRUE)
# parsing and displaying the camapign data
head(df)
## $status
## [1] "OK"
##
## $copyright
## [1] "Copyright (c) 2015 The New York Times Company. All Rights Reserved."
##
## $results
## $results[[1]]
## $results[[1]]$candidate_name
## [1] "Obama, Barack"
##
## $results[[1]]$name
## [1] "Barack Obama"
##
## $results[[1]]$candidate_id
## [1] "P80003338"
##
## $results[[1]]$committee_id
## [1] "C00431445"
##
## $results[[1]]$associated_committees
## list()
##
## $results[[1]]$party
## [1] "D"
##
## $results[[1]]$total_receipts
## [1] "770469844.0"
##
## $results[[1]]$total_disbursements
## [1] "740557859.5"
##
## $results[[1]]$individual_contributions
## [1] "662225678.0"
##
## $results[[1]]$cash_on_hand
## [1] "29911984.0"
##
## $results[[1]]$net_individual_contributions
## [1] "656610810.0"
##
## $results[[1]]$net_party_contributions
## [1] "850.0"
##
## $results[[1]]$net_pac_contributions
## [1] "1580.0"
##
## $results[[1]]$net_candidate_contributions
## [1] "0.0"
##
## $results[[1]]$total_contributions
## [1] "662239758.0"
##
## $results[[1]]$contributions_200_499
## [1] "69883814.0"
##
## $results[[1]]$transfers_in
## [1] "83450000.0"
##
## $results[[1]]$federal_funds
## [1] "0.0"
##
## $results[[1]]$total_contributions_less_than_200
## [1] "291502143.5"
##
## $results[[1]]$total_contributions_max
## [1] "98960776.0"
##
## $results[[1]]$net_primary_contributions
## [1] "-145109419.4"
##
## $results[[1]]$net_general_contributions
## [1] "175021403.4"
##
## $results[[1]]$total_refunds
## [1] "5626510.54"
##
## $results[[1]]$contributions_500_1499
## [1] "115620433.3"
##
## $results[[1]]$date_coverage_from
## [1] "2007-01-01"
##
## $results[[1]]$date_coverage_to
## [1] "2008-11-24"
##
## $results[[1]]$cash_on_hand_party_rank
## [1] 1
##
## $results[[1]]$total_receipts_party_rank
## [1] 1
##
## $results[[1]]$total_disbursements_party_rank
## [1] 1
##
## $results[[1]]$contributions_less_than_200_party_rank
## [1] 1
##
## $results[[1]]$contributions_200_499_party_rank
## [1] 1
##
## $results[[1]]$contributions_500_1499_party_rank
## [1] 1
##
## $results[[1]]$contributions_1500_2499_party_rank
## [1] 1
##
## $results[[1]]$contributions_max_party_rank
## [1] 1
##
## $results[[1]]$summary_note
## NULL
##
## $results[[1]]$contributions_1500_2499
## [1] "18924824.06"
##
## $results[[1]]$net_primary_party_rank
## NULL
##
## $results[[1]]$contributions_max
## [1] "98960776.0"
##
## $results[[1]]$weekly_note
## NULL
##
## $results[[1]]$contribution_size_note
## NULL
##
## $results[[1]]$geographic_note
## NULL