Import your data

data("mtcars")
mtcars <- as_tibble(mtcars)

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) 
## # A tibble: 32 × 1
##      mpg
##    <dbl>
##  1  21  
##  2  21  
##  3  22.8
##  4  21.4
##  5  18.7
##  6  18.1
##  7  14.3
##  8  24.4
##  9  22.8
## 10  19.2
## # … with 22 more rows
mtcars %>% select(mpg)
## # A tibble: 32 × 1
##      mpg
##    <dbl>
##  1  21  
##  2  21  
##  3  22.8
##  4  21.4
##  5  18.7
##  6  18.1
##  7  14.3
##  8  24.4
##  9  22.8
## 10  19.2
## # … with 22 more rows

Create your own function

#double values in a column
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
mtcars %>% map(double_by_factor, 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)
## # A tibble: 3 × 1
##     cyl
##   <dbl>
## 1     6
## 2     4
## 3     8
mtcars %>%
    
    #Split into a list of data frames
    split(.$cyl) %>%
    
    #Repeat regression over each group 
    map(~lm(formula = mpg ~ wt, data = .x)) %>%
    #Extract coefficients from regression results
    map(broom::tidy, conf.int = TRUE) %>%
    # Convert to tibble 
    
    bind_rows(.id = "cyl") %>%
    
    # Filter for coefficients 
        filter(term == "wt")
## # A tibble: 3 × 8
##   cyl   term  estimate std.error statistic p.value conf.low conf.high
##   <chr> <chr>    <dbl>     <dbl>     <dbl>   <dbl>    <dbl>     <dbl>
## 1 4     wt       -5.65     1.85      -3.05  0.0137    -9.83    -1.46 
## 2 6     wt       -2.78     1.33      -2.08  0.0918    -6.21     0.651
## 3 8     wt       -2.19     0.739     -2.97  0.0118    -3.80    -0.582
  # reg_coeff_tbl   %>%    
    #mutate(estimate = -estimate,
          # conf.low = -conf.low,
           #conf.high = -conf.high) %>%
    
    #ggplot(aes(x = -esitmate, 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

data <- read_csv("../00_data/myData.csv") 
## Rows: 1222 Columns: 10
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): months, state
## dbl (8): year, colony_n, colony_max, colony_lost, colony_lost_pct, colony_ad...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
data %>% lm(formula = colony_n ~ colony_lost_pct, data = .)
## 
## Call:
## lm(formula = colony_n ~ colony_lost_pct, data = .)
## 
## Coefficients:
##     (Intercept)  colony_lost_pct  
##          100888             2047
data %>% distinct(year)
## # A tibble: 7 × 1
##    year
##   <dbl>
## 1  2015
## 2  2016
## 3  2017
## 4  2018
## 5  2019
## 6  2020
## 7  2021
data %>%
#Split into a list of data frames
     split(.$year) %>%
    
    #Repeat regression over each group 
     map(~lm(formula = colony_n ~ colony_lost_pct, data = .x)) %>%
    #Extract coefficients from regression results
     map(broom::tidy, conf.int = TRUE) %>%
        # Convert to tibble 
    
    bind_rows(.id = "year") %>%
    
    # Filter for coefficients 
        filter(term == "colony_lost_pct")
## # A tibble: 7 × 8
##   year  term            estimate std.error statistic p.value conf.low conf.high
##   <chr> <chr>              <dbl>     <dbl>     <dbl>   <dbl>    <dbl>     <dbl>
## 1 2015  colony_lost_pct   2470.      4040.    0.612    0.542   -5499.    10440.
## 2 2016  colony_lost_pct   5004.      5078.    0.985    0.326   -5014.    15022.
## 3 2017  colony_lost_pct    -56.1     4234.   -0.0132   0.989   -8410.     8298.
## 4 2018  colony_lost_pct    694.      3946.    0.176    0.861   -7091.     8478.
## 5 2019  colony_lost_pct   2810.      5685.    0.494    0.622   -8431.    14052.
## 6 2020  colony_lost_pct   4533.      5095.    0.890    0.375   -5519.    14584.
## 7 2021  colony_lost_pct   -460.      6653.   -0.0691   0.945  -13676.    12756.