library(gapminder)
library(dplyr)
# closest countries in terms of life expectancy over time
closest <- gapminder %>%
pairwise_dist(country, year, lifeExp) %>%
arrange(distance)
closest
closest %>%
filter(item1 == "United States")
# to remove duplicates, use upper = FALSE
gapminder %>%
pairwise_dist(country, year, lifeExp, upper = FALSE) %>%
arrange(distance)
# Can also use Manhattan distance
gapminder %>%
pairwise_dist(country, year, lifeExp, method = "manhattan", upper = FALSE) %>%
arrange(distance)
Run the code above in your browser using DataLab