Import your data

# excel file
refugee <- read_excel("../00_data/mydata.xlsx") %>% janitor::clean_names()

Repeat the same operation over different columns of a data frame

Case of numeric variables

mtcars %>% map_dbl(.x = ., .f =~mean(x = .x))
##        mpg        cyl       disp         hp       drat         wt       qsec 
##  20.090625   6.187500 230.721875 146.687500   3.596563   3.217250  17.848750 
##         vs         am       gear       carb 
##   0.437500   0.406250   3.687500   2.812500
mtcars %>% map_dbl(.f = ~mean(x = .x))
##        mpg        cyl       disp         hp       drat         wt       qsec 
##  20.090625   6.187500 230.721875 146.687500   3.596563   3.217250  17.848750 
##         vs         am       gear       carb 
##   0.437500   0.406250   3.687500   2.812500
mtcars %>% map_dbl(mean)
##        mpg        cyl       disp         hp       drat         wt       qsec 
##  20.090625   6.187500 230.721875 146.687500   3.596563   3.217250  17.848750 
##         vs         am       gear       carb 
##   0.437500   0.406250   3.687500   2.812500
#adding an argument
mtcars %>% map_dbl(.x = ., .f =~mean(x = .x, trim = 0.1))
##         mpg         cyl        disp          hp        drat          wt 
##  19.6961538   6.2307692 222.5230769 141.1923077   3.5792308   3.1526923 
##        qsec          vs          am        gear        carb 
##  17.8276923   0.4230769   0.3846154   3.6153846   2.6538462
mtcars %>% map_dbl(mean, trim = 0.1)
##         mpg         cyl        disp          hp        drat          wt 
##  19.6961538   6.2307692 222.5230769 141.1923077   3.5792308   3.1526923 
##        qsec          vs          am        gear        carb 
##  17.8276923   0.4230769   0.3846154   3.6153846   2.6538462
mtcars %>% select(.data = ., mpg)
##                      mpg
## Mazda RX4           21.0
## Mazda RX4 Wag       21.0
## Datsun 710          22.8
## Hornet 4 Drive      21.4
## Hornet Sportabout   18.7
## Valiant             18.1
## Duster 360          14.3
## Merc 240D           24.4
## Merc 230            22.8
## Merc 280            19.2
## Merc 280C           17.8
## Merc 450SE          16.4
## Merc 450SL          17.3
## Merc 450SLC         15.2
## Cadillac Fleetwood  10.4
## Lincoln Continental 10.4
## Chrysler Imperial   14.7
## Fiat 128            32.4
## Honda Civic         30.4
## Toyota Corolla      33.9
## Toyota Corona       21.5
## Dodge Challenger    15.5
## AMC Javelin         15.2
## Camaro Z28          13.3
## Pontiac Firebird    19.2
## Fiat X1-9           27.3
## Porsche 914-2       26.0
## Lotus Europa        30.4
## Ford Pantera L      15.8
## Ferrari Dino        19.7
## Maserati Bora       15.0
## Volvo 142E          21.4
mtcars %>% select(mpg)
##                      mpg
## Mazda RX4           21.0
## Mazda RX4 Wag       21.0
## Datsun 710          22.8
## Hornet 4 Drive      21.4
## Hornet Sportabout   18.7
## Valiant             18.1
## Duster 360          14.3
## Merc 240D           24.4
## Merc 230            22.8
## Merc 280            19.2
## Merc 280C           17.8
## Merc 450SE          16.4
## Merc 450SL          17.3
## Merc 450SLC         15.2
## Cadillac Fleetwood  10.4
## Lincoln Continental 10.4
## Chrysler Imperial   14.7
## Fiat 128            32.4
## Honda Civic         30.4
## Toyota Corolla      33.9
## Toyota Corona       21.5
## Dodge Challenger    15.5
## AMC Javelin         15.2
## Camaro Z28          13.3
## Pontiac Firebird    19.2
## Fiat X1-9           27.3
## Porsche 914-2       26.0
## Lotus Europa        30.4
## Ford Pantera L      15.8
## Ferrari Dino        19.7
## Maserati Bora       15.0
## Volvo 142E          21.4

Create your own function

double_by_factor <- function(x, factor) {x * factor}

10 %>% double_by_factor(factor = 2)
## [1] 20
mtcars %>% map(.x = ., .f = ~double_by_factor(x = .x, factor = 10))
## $mpg
##  [1] 210 210 228 214 187 181 143 244 228 192 178 164 173 152 104 104 147 324 304
## [20] 339 215 155 152 133 192 273 260 304 158 197 150 214
## 
## $cyl
##  [1] 60 60 40 60 80 60 80 40 40 60 60 80 80 80 80 80 80 40 40 40 40 80 80 80 80
## [26] 40 40 40 80 60 80 40
## 
## $disp
##  [1] 1600 1600 1080 2580 3600 2250 3600 1467 1408 1676 1676 2758 2758 2758 4720
## [16] 4600 4400  787  757  711 1201 3180 3040 3500 4000  790 1203  951 3510 1450
## [31] 3010 1210
## 
## $hp
##  [1] 1100 1100  930 1100 1750 1050 2450  620  950 1230 1230 1800 1800 1800 2050
## [16] 2150 2300  660  520  650  970 1500 1500 2450 1750  660  910 1130 2640 1750
## [31] 3350 1090
## 
## $drat
##  [1] 39.0 39.0 38.5 30.8 31.5 27.6 32.1 36.9 39.2 39.2 39.2 30.7 30.7 30.7 29.3
## [16] 30.0 32.3 40.8 49.3 42.2 37.0 27.6 31.5 37.3 30.8 40.8 44.3 37.7 42.2 36.2
## [31] 35.4 41.1
## 
## $wt
##  [1] 26.20 28.75 23.20 32.15 34.40 34.60 35.70 31.90 31.50 34.40 34.40 40.70
## [13] 37.30 37.80 52.50 54.24 53.45 22.00 16.15 18.35 24.65 35.20 34.35 38.40
## [25] 38.45 19.35 21.40 15.13 31.70 27.70 35.70 27.80
## 
## $qsec
##  [1] 164.6 170.2 186.1 194.4 170.2 202.2 158.4 200.0 229.0 183.0 189.0 174.0
## [13] 176.0 180.0 179.8 178.2 174.2 194.7 185.2 199.0 200.1 168.7 173.0 154.1
## [25] 170.5 189.0 167.0 169.0 145.0 155.0 146.0 186.0
## 
## $vs
##  [1]  0  0 10 10  0 10  0 10 10 10 10  0  0  0  0  0  0 10 10 10 10  0  0  0  0
## [26] 10  0 10  0  0  0 10
## 
## $am
##  [1] 10 10 10  0  0  0  0  0  0  0  0  0  0  0  0  0  0 10 10 10  0  0  0  0  0
## [26] 10 10 10 10 10 10 10
## 
## $gear
##  [1] 40 40 40 30 30 30 30 40 40 40 40 30 30 30 30 30 30 40 40 40 30 30 30 30 30
## [26] 40 50 50 50 50 50 40
## 
## $carb
##  [1] 40 40 10 10 20 10 40 20 20 40 40 30 30 30 40 40 40 10 20 10 10 20 20 40 20
## [26] 10 20 20 40 60 80 20

Repeat the same operation over different elements of a list

When you have a grouping variable (factor)

mtcars %>% lm(formula = mpg ~ wt, data = .)
## 
## Call:
## lm(formula = mpg ~ wt, data = .)
## 
## Coefficients:
## (Intercept)           wt  
##      37.285       -5.344
mtcars %>% distinct(cyl)
##                   cyl
## Mazda RX4           6
## Datsun 710          4
## Hornet Sportabout   8
reg_coeff_tbl <- mtcars %>%
    #split
    split(.$cyl) %>%
    #repeat
    map(~lm(formula = mpg ~ wt, data = .x)) %>%
    map(broom::tidy, conf.int = TRUE) %>%
#convert
bind_rows(.id = "cyl")

#filter
reg_coeff_tbl %>%
    mutate(estimate = -estimate)%>%

ggplot(aes(x = estimate, y = cyl)) +
    geom_point() +
    geom_errorbar(aes(xmin = conf.low, xmax = conf.high))

Create your own

Choose either one of the two cases above and apply it to your data

refugee %>% distinct(country_of_asylum_iso_code)
## # A tibble: 189 × 1
##    country_of_asylum_iso_code
##    <chr>                     
##  1 AFG                       
##  2 ALB                       
##  3 DZA                       
##  4 AGO                       
##  5 EGY                       
##  6 ARG                       
##  7 ARM                       
##  8 AUS                       
##  9 AUT                       
## 10 AZE                       
## # ℹ 179 more rows