Last active
April 2, 2018 20:06
-
-
Save gkampolis/0cffa66221cb5bd0b25fdac7df7e451d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
library(gapminder) | |
library(tidyverse) | |
##====================================================================== | |
## Returns the first 5 countries with the worst drop in life expectancy. | |
##====================================================================== | |
gapminder %>% | |
select(country, year, continent, lifeExp) %>% | |
group_by(continent, country) %>% | |
mutate(le_delta = lifeExp - lag(lifeExp)) %>% | |
summarize(worst_le_delta = min(le_delta, na.rm = TRUE)) %>% | |
arrange(worst_le_delta) %>% | |
head(5) | |
##=================================== | |
## The above results in: | |
##=================================== | |
# A tibble: 5 x 3 | |
# Groups: continent [1] | |
# continent country worst_le_delta | |
# <fct> <fct> <dbl> | |
#1 Africa Rwanda -20.4 | |
#2 Africa Zimbabwe -13.6 | |
#3 Africa Lesotho -11.0 | |
#4 Africa Swaziland -10.4 | |
#5 Africa Botswana -10.2 | |
##=================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment