Created
November 5, 2020 23:21
-
-
Save rasmusab/7fc423a2274cc0b0dd905e4fdcd96227 to your computer and use it in GitHub Desktop.
This script pulls down information on all Nobel prize winners
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This R script pulls down information on all Nobel prize winners | |
# from the official Nobel Prize API and selects a subset of this | |
# information that is then saves to a CSV-file. | |
# By Rasmus Bååth, 2020, in the public domain. | |
library(tidyverse) | |
library(httr) | |
nobel_response <- GET("http://api.nobelprize.org/2.0/laureates?limit=100000&format=json") | |
nobel_raw <- as_tibble(content(nobel_response, simplifyVector = TRUE, flatten = TRUE)$laureates) | |
nobel <- nobel_raw %>% | |
select(-starts_with("links.")) %>% | |
unnest(nobelPrizes) %>% | |
rowwise() %>% | |
mutate( | |
organization_name = ifelse(is.null(affiliations$nameNow.en), NA, affiliations$nameNow.en[1]), | |
organization_city = ifelse(is.null(affiliations$cityNow.en), NA, affiliations$cityNow.en[1]), | |
organization_country = ifelse(is.null(affiliations$countryNow.en), NA, affiliations$countryNow.en[1]) | |
) %>% | |
ungroup() %>% | |
mutate( | |
awardYear = as.integer(awardYear), | |
id = as.integer(id), | |
birth.date = as.Date(str_replace_all(birth.date, "-00", "-01")), | |
death.date = as.Date(str_replace_all(death.date, "-00", "-01")), | |
prize = paste(categoryFullName.en, awardYear), | |
laureate_type = ifelse(!is.na(knownName.en), "Individual", "Organization"), | |
full_name = coalesce(knownName.en, orgName.en), | |
prize_share = recode(portion, "1" = "1/1"), | |
category.en = recode(category.en, | |
"Physiology or Medicine" = "Medicin", | |
"Economic Sciences" = "Economics") | |
) %>% | |
select( | |
year = awardYear, | |
category = category.en, | |
prize, | |
motivation = motivation.en, | |
prize_share, | |
laureate_id = id, | |
laureate_type, | |
full_name, | |
birth_date = birth.date, | |
birth_city = birth.place.cityNow.se, | |
birth_country = birth.place.countryNow.en, | |
gender, | |
organization_name, | |
organization_city, | |
organization_country , | |
death_date = death.date, | |
death_city = death.place.cityNow.en, | |
death_country = death.place.countryNow.en) %>% | |
arrange(year, category) | |
write_csv(nobel, "scripts/nobel.csv") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment