Import your data

bee_colony <- read_excel("../00_data/MyData3.xlsx")

Repeat the same operation over different columns of a data frame

Case of numeric variables

bee_colony %>% select(where(is.numeric)) %>% map_dbl(.x = ., .f = ~mean(x = .x, na.rm = TRUE))
##            year     colony_size     colony_lost colony_lost_pct 
##      2017.76923    123578.04255     16551.31915        11.38185
bee_colony %>% select(where(is.numeric)) %>% map_dbl(.f = ~mean(x = .x, na.rm = TRUE))
##            year     colony_size     colony_lost colony_lost_pct 
##      2017.76923    123578.04255     16551.31915        11.38185
bee_colony %>% select(where(is.numeric)) %>% map_dbl(.f = mean, na.rm = TRUE)
##            year     colony_size     colony_lost colony_lost_pct 
##      2017.76923    123578.04255     16551.31915        11.38185
 # Adding an argument
bee_colony %>% select(where(is.numeric)) %>% map_dbl(.x = ., .f = ~mean(x = .x, trim = 0.1, na.rm = TRUE))
##            year     colony_size     colony_lost colony_lost_pct 
##      2017.74029     32420.09564      4038.61849        10.56624
bee_colony %>% select(where(is.numeric)) %>% map_dbl(mean, trim = 0.1, na.rm = TRUE)
##            year     colony_size     colony_lost colony_lost_pct 
##      2017.74029     32420.09564      4038.61849        10.56624
bee_colony %>% select(.data = ., year)
## # A tibble: 1,222 × 1
##     year
##    <dbl>
##  1  2015
##  2  2015
##  3  2015
##  4  2015
##  5  2015
##  6  2015
##  7  2015
##  8  2015
##  9  2015
## 10  2015
## # ℹ 1,212 more rows
bee_colony %>% select(year)
## # A tibble: 1,222 × 1
##     year
##    <dbl>
##  1  2015
##  2  2015
##  3  2015
##  4  2015
##  5  2015
##  6  2015
##  7  2015
##  8  2015
##  9  2015
## 10  2015
## # ℹ 1,212 more rows

Creating my own function

 # Double values in columns
double_by_factor <- function(x, factor) {x * factor}
10 %>% double_by_factor(factor = 2)
## [1] 20
bee_colony %>% select(where(is.numeric)) %>% map_dfr(.x = ., .f = ~double_by_factor(x = .x, factor = 10))
## # A tibble: 1,222 × 4
##     year colony_size colony_lost colony_lost_pct
##    <dbl>       <dbl>       <dbl>           <dbl>
##  1 20150       70000       18000             260
##  2 20150      350000       46000             130
##  3 20150      130000       15000             110
##  4 20150    14400000     2550000             150
##  5 20150       35000       15000             120
##  6 20150       39000        8700             220
##  7 20150     3050000      420000             130
##  8 20150     1040000      145000             140
##  9 20150      105000        3800              40
## 10 20150      810000       37000              40
## # ℹ 1,212 more rows
bee_colony %>% select(where(is.numeric)) %>% map_dfr(double_by_factor, factor = 2)
## # A tibble: 1,222 × 4
##     year colony_size colony_lost colony_lost_pct
##    <dbl>       <dbl>       <dbl>           <dbl>
##  1  4030       14000        3600              52
##  2  4030       70000        9200              26
##  3  4030       26000        3000              22
##  4  4030     2880000      510000              30
##  5  4030        7000        3000              24
##  6  4030        7800        1740              44
##  7  4030      610000       84000              26
##  8  4030      208000       29000              28
##  9  4030       21000         760               8
## 10  4030      162000        7400               8
## # ℹ 1,212 more rows