Import the tidyverse and load up the data
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ ggplot2 3.3.3 ✔ purrr 0.3.4
## ✔ tibble 3.1.1 ✔ dplyr 1.0.5
## ✔ tidyr 1.1.3 ✔ stringr 1.4.0
## ✔ readr 1.4.0 ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
census <- tibble("City"=c("Paris", "Paris", "Paris", "Paris",
"London", "London", "London", "London",
"Rome", "Rome", "Rome", "Rome"),
"year"=as.integer(c(2001, 2008, 2009, 2010,
2001, 2006, 2011, 2015,
2001, 2006, 2009, 2012)),
"pop"=c(2.148, 2.211, 2.234, 2.244,
7.322, 7.657, 8.174, 8.615,
2.547, 2.627, 2.734, 2.627))
We start by grabbing the data for the year we care about
census %>% filter(year==2001)
We can see that the smallest population was in Paris that year but let’s try to extract it using R.
pop <- (census %>% filter(year==2001))$pop
pop
## [1] 2.148 7.322 2.547
The min
function returns the minimum of a list of numbers. If we run this on pop
then we will get the smallest number.
min_pop <- min(pop)
min_pop
## [1] 2.148
We can now use this minimum population to further filter the census data;
census %>% filter(year==2001) %>% filter(pop==min_pop)
Finally(!) we can extract the City column
(census %>% filter(year==2001) %>% filter(pop==min_pop))["City"]
All of this could be combined into a single (dense) expression, e.g.
city <- (census %>%
filter(year==2001) %>%
filter(pop==min((census %>% filter(year==2001))["pop"]))
)["City"]
city