Project Checkpoint 3
Setup
For simplicity here, I will study only the top 10 countries in terms of female medals. This code is from last week’s project step.
New data from API
I’m choosing to use the API-Ninjas API for country populations. This API requires an account and API key. In the code that you see below, I have replaced hidden the line that stores my API key, so that it is not publicly shared.
get_population <- function(country, key = my_key)
{
url <- glue("https://api.api-ninjas.com/v1/population?country={country};X-Api-Key={key}")
my_data <- safely(fromJSON)(url)
if (is.null(my_data$result)) {
return(NA)
} else {
return(my_data$result$historical_population)
}
}
get_population("Japan")
get_population("United States")Since “United States” has a space in it, it isn’t being searched properly by the API URL. After some investigation on the API site and experimentation, I see that I can use country abbreviations instead.
Now let’s make this a tibble so we can join it to our original data:
Now, we join the datasets:
Note: There are a lot of NAs because the population data doesn’t go as far back in time as the olympic data!
Finally, we use our new information to produce an interesting summary: