library(jsonlite)
library(DT)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter()  masks stats::filter()
## ✖ purrr::flatten() masks jsonlite::flatten()
## ✖ dplyr::lag()     masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
laureate_url <- "http://api.nobelprize.org/v1/laureate.json"
prize_url <- "http://api.nobelprize.org/v1/prize.json"

# Gather laureate data
laureate_raw_data <- fromJSON(laureate_url) %>% as.data.frame()

# Gather  prize data
raw_prize_data <- fromJSON(prize_url) %>% as.data.frame()

# create a DT table with ellipsis for long text entries
create_ellipsis_table <- function(data) {
  datatable(
    head(data, 50),
    plugins = "ellipsis",
    options = list(
      scrollX = TRUE,
      columnDefs = list(list(
        targets = "_all",
        render = JS("$.fn.dataTable.render.ellipsis(30, false)")
      ))
    )
  )
}

prize_data <- raw_prize_data %>%
  mutate_if(is.list, map, as_data_frame) %>%
  unnest()
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `prizes.laureates = (function (.x, .f, ..., .progress = FALSE)
##   ...`.
## Caused by warning:
## ! `as_data_frame()` was deprecated in tibble 2.0.0.
## ℹ Please use `as_tibble()` (with slightly different semantics) to convert to a
##   tibble, or `as.data.frame()` to convert to a data frame.
## ℹ The deprecated feature was likely used in the purrr package.
##   Please report the issue at <https://github.com/tidyverse/purrr/issues>.
## Warning: `cols` is now required when using `unnest()`.
## ℹ Please use `cols = c(prizes.laureates)`.
colnames(prize_data) <- c("year", "category", "id", "firstname", "surname", "motivation", "share", "overall_motivation")

create_ellipsis_table(prize_data)
laureate_data <- laureate_raw_data %>%
  mutate_if(is.list, map, as_data_frame) %>%
  unnest()
## Warning: `cols` is now required when using `unnest()`.
## ℹ Please use `cols = c(laureates.prizes)`.
colnames(laureate_data) <- c("id", "firstname", "surname", "born", "death", "born_country", "born_country_code", "born_city", "death_country", "death_country_code", "death_city", "gender", "prize_year", "prize_category", "prize_share", "prize_motivation", "affiliations", "overallMotivation")

create_ellipsis_table(laureate_data)

1. Which country has the most Nobel laureates who were born there but received their Nobel prize as a citizen of a different country?

# laureates who were born in one country but received the prize as a citizen of another country

born_and_citizenship <- laureate_data %>%
  filter(born_country != death_country)

#  laureates by their birth country
born_and_citizenship_counts <- born_and_citizenship %>%
  count(born_country)

# country with the most laureates
country_with_most_laureates <- born_and_citizenship_counts %>%
  arrange(desc(n)) %>%
  head(1)

print(country_with_most_laureates)
## # A tibble: 1 × 2
##   born_country     n
##   <chr>        <int>
## 1 Germany         34

2. How many Nobel prizes have been awarded in each category?

prizes_per_category <- prize_data %>%
  count(category)

print(prizes_per_category)
## # A tibble: 6 × 2
##   category       n
##   <chr>      <int>
## 1 chemistry    194
## 2 economics     93
## 3 literature   120
## 4 medicine     227
## 5 peace        141
## 6 physics      225

3. Who were the first Nobel laureates?

# Find the first Nobel laureates

first_nobel_laureates <- laureate_data %>%
  filter(prize_year == min(prize_year)) %>%
  select(firstname, surname, prize_year, prize_category)

print(first_nobel_laureates)
## # A tibble: 6 × 4
##   firstname      surname     prize_year prize_category
##   <chr>          <chr>       <chr>      <chr>         
## 1 Wilhelm Conrad Röntgen     1901       physics       
## 2 Jacobus H.     van 't Hoff 1901       chemistry     
## 3 Emil           von Behring 1901       medicine      
## 4 Henry          Dunant      1901       peace         
## 5 Frédéric       Passy       1901       peace         
## 6 Sully          Prudhomme   1901       literature

4. What is the average age of Nobel laureates when they receive their Nobel Prize?

# Calculate the age of Nobel laureates when they received the prize
laureate_data_age <- laureate_data %>%
  mutate(birth_year = as.numeric(substr(born, 1, 4)),
         prize_year = as.numeric(prize_year),
         age_at_prize = prize_year - birth_year)

# Calculate the average age
average_age_at_prize <- mean(laureate_data_age$age_at_prize, na.rm = TRUE)

cat('The average age of Nobel laureates when they receive their Nobel Prize is', average_age_at_prize)
## The average age of Nobel laureates when they receive their Nobel Prize is 59.76877