Skip to content

Instantly share code, notes, and snippets.

@seankross
Last active January 31, 2016 07:51
Show Gist options
  • Select an option

  • Save seankross/75c24c3bcffbf740e91f to your computer and use it in GitHub Desktop.

Select an option

Save seankross/75c24c3bcffbf740e91f to your computer and use it in GitHub Desktop.
We The People
library(dplyr)
library(jsonlite)
pets <- list()
for(i in 1:6){
offset_ <- seq(0, 5000, by = 1000)
query_ <- paste0("https://api.whitehouse.gov/v1/petitions.json?limit=1000&offset=", offset_[i])
pets[[i]] <- fromJSON(query_)[[2]]
Sys.sleep(0.5)
}
petitions_raw <- do.call("rbind", pets)
get_id1 <- function(l){
lapply(l, function(df) {
df$id[1]
})
}
get_id2 <- function(l){
lapply(l, function(df) {
df$id[2]
})
}
get_id3 <- function(l){
lapply(l, function(df) {
df$id[3]
})
}
get_name1 <- function(l){
lapply(l, function(df) {
df$name[1]
})
}
get_name2 <- function(l){
lapply(l, function(df) {
df$name[2]
})
}
get_name3 <- function(l){
lapply(l, function(df) {
df$name[3]
})
}
get_response_id <- function(l){
lapply(l, function(df){
ifelse(is.null(df$id), NA, df$id)
})
}
get_response_url <- function(l){
lapply(l, function(df){
ifelse(is.null(df$url), NA, df$url)
})
}
get_response_associationTime <- function(l){
lapply(l, function(df){
ifelse(is.null(df$associationTime), NA, df$associationTime)
})
}
petitions <- petitions_raw %>%
mutate(id1 = get_id1(issues)) %>%
mutate(id2 = get_id2(issues)) %>%
mutate(id3 = get_id3(issues)) %>%
mutate(name1 = get_name1(issues)) %>%
mutate(name2 = get_name2(issues)) %>%
mutate(name3 = get_name3(issues)) %>%
select(-issues) %>%
mutate(response_id = get_response_id(response)) %>%
mutate(response_url = get_response_url(response)) %>%
mutate(response_associationTime = get_response_associationTime(response)) %>%
select(-response)
petitions <- data.frame(lapply(petitions, as.character), stringsAsFactors=FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment