Skip to content

Instantly share code, notes, and snippets.

@gkampolis
Last active April 2, 2018 20:06
Show Gist options
  • Save gkampolis/0cffa66221cb5bd0b25fdac7df7e451d to your computer and use it in GitHub Desktop.
Save gkampolis/0cffa66221cb5bd0b25fdac7df7e451d to your computer and use it in GitHub Desktop.
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