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(.f = ~mean(x = .x))
## $mpg
## [1] 20.09062
## 
## $cyl
## [1] 6.1875
## 
## $disp
## [1] 230.7219
## 
## $hp
## [1] 146.6875
## 
## $drat
## [1] 3.596563
## 
## $wt
## [1] 3.21725
## 
## $qsec
## [1] 17.84875
## 
## $vs
## [1] 0.4375
## 
## $am
## [1] 0.40625
## 
## $gear
## [1] 3.6875
## 
## $carb
## [1] 2.8125
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
## # ℹ 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
## # ℹ 22 more rows

Create your own function

artists %>% map_dbl(.x = ., .f = ~ mean(x = .x, trim = 0.1, na.rm = TRUE))
##             state              race              type     all_workers_n 
##                NA                NA                NA      3.305271e+05 
##         artists_n     artists_share location_quotient 
##      2.689338e+02      8.777772e-04      9.139559e-01
artists %>% map_dbl(mean, trim = 0.1, na.rm = TRUE) 
##             state              race              type     all_workers_n 
##                NA                NA                NA      3.305271e+05 
##         artists_n     artists_share location_quotient 
##      2.689338e+02      8.777772e-04      9.139559e-01

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
reg_coeff_tbl <- 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 wt coefficients
    filter(term == "wt")
reg_coeff_tbl %>%
    
    mutate(estimate = -estimate,
           conf.low = - conf.low,
           conf.high = - conf.high) %>%
    
    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

artists %>% lm(formula = artists_n ~ all_workers_n, data = .)
## 
## Call:
## lm(formula = artists_n ~ all_workers_n, data = .)
## 
## Coefficients:
##   (Intercept)  all_workers_n  
##    -1.739e+02      1.381e-03
artists %>% distinct(state)
## # A tibble: 52 × 1
##    state               
##    <chr>               
##  1 Alabama             
##  2 Alaska              
##  3 Arizona             
##  4 Arkansas            
##  5 California          
##  6 Colorado            
##  7 Connecticut         
##  8 Delaware            
##  9 District of Columbia
## 10 Florida             
## # ℹ 42 more rows
reg_coeff_tbl <- artists %>%
    
    # Split it into a list of data frames
    split(.$state) %>%
    
    # Repeat regression over each group 
    map(~lm(formula = artists_n ~ all_workers_n, data = .)) %>%
    
    # Extract coefficients from regression results 
    map(broom::tidy, conf.int = TRUE, na.rm = TRUE) %>%
    
    # Convert to tibble 
    bind_rows(.id = "state")
reg_coeff_tbl %>%
    
    mutate( estimate = -estimate,
            conf.low = -conf.low,
           conf.high = -conf.high) %>%
    
    ggplot(aes(x = estimate, y = state)) +
    geom_point() +
    geom_errorbar(aes(xmin = conf.low, xmax = conf.high))