library(tidyverse)
library(magrittr)
library(gapminder)


#Pipe
#More readable code
#- avoids nested functions and too many variables between the steps
#- makes it easy to add steps in the middle of the code

#Magrittr offers several pipe types, but to use the basic one %>%, you do not 
#have to load magrittr. It is included in dplyr. 

#Too many variables Example
my_gap <- gapminder
gap_asia44 <- filter(gapminder, continent == "Asia", year == "1952")
gap_asia44 <- slice_max(gap_asia44, order_by = pop, n = 5) #gives rows with 5 highest values of that variable

#Nested functions Example (you need fewer variables, but...)
top5pop2 <- slice_max(filter(gapminder, continent == "Asia", 
                             year == "1952"),
                      order_by = pop, n = 5)

#Pipe 
top5pop2 <- gapminder %>% 
  filter(continent == "Asia", pop > mean(.$pop)) 








