Import your data

data("mtcars")
mtcars <- as_tibble(mtcars)
skimr::skim(mtcars)
Data summary
Name mtcars
Number of rows 32
Number of columns 11
_______________________
Column type frequency:
numeric 11
________________________
Group variables None

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
mpg 0 1 20.09 6.03 10.40 15.43 19.20 22.80 33.90 ▃▇▅▁▂
cyl 0 1 6.19 1.79 4.00 4.00 6.00 8.00 8.00 ▆▁▃▁▇
disp 0 1 230.72 123.94 71.10 120.83 196.30 326.00 472.00 ▇▃▃▃▂
hp 0 1 146.69 68.56 52.00 96.50 123.00 180.00 335.00 ▇▇▆▃▁
drat 0 1 3.60 0.53 2.76 3.08 3.70 3.92 4.93 ▇▃▇▅▁
wt 0 1 3.22 0.98 1.51 2.58 3.33 3.61 5.42 ▃▃▇▁▂
qsec 0 1 17.85 1.79 14.50 16.89 17.71 18.90 22.90 ▃▇▇▂▁
vs 0 1 0.44 0.50 0.00 0.00 0.00 1.00 1.00 ▇▁▁▁▆
am 0 1 0.41 0.50 0.00 0.00 0.00 1.00 1.00 ▇▁▁▁▆
gear 0 1 3.69 0.74 3.00 3.00 4.00 4.00 5.00 ▇▁▆▁▂
carb 0 1 2.81 1.62 1.00 2.00 2.00 4.00 8.00 ▇▂▅▁▁
mtcars %>% distinct(cyl)
## # A tibble: 3 × 1
##     cyl
##   <dbl>
## 1     6
## 2     4
## 3     8

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, 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
#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)
##        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 %>% 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

#Double value in columns
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_dfr(.x = ., .f = ~double_by_factor(x = .x, factor = 10))
## # A tibble: 32 × 11
##      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
##    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
##  1   210    60  1600  1100  39    26.2  165.     0    10    40    40
##  2   210    60  1600  1100  39    28.8  170.     0    10    40    40
##  3   228    40  1080   930  38.5  23.2  186.    10    10    40    10
##  4   214    60  2580  1100  30.8  32.2  194.    10     0    30    10
##  5   187    80  3600  1750  31.5  34.4  170.     0     0    30    20
##  6   181    60  2250  1050  27.6  34.6  202.    10     0    30    10
##  7   143    80  3600  2450  32.1  35.7  158.     0     0    30    40
##  8   244    40  1467   620  36.9  31.9  200     10     0    40    20
##  9   228    40  1408   950  39.2  31.5  229     10     0    40    20
## 10   192    60  1676  1230  39.2  34.4  183     10     0    40    40
## # ℹ 22 more rows
mtcars %>% map_dfr(double_by_factor, factor = 10)
## # A tibble: 32 × 11
##      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
##    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
##  1   210    60  1600  1100  39    26.2  165.     0    10    40    40
##  2   210    60  1600  1100  39    28.8  170.     0    10    40    40
##  3   228    40  1080   930  38.5  23.2  186.    10    10    40    10
##  4   214    60  2580  1100  30.8  32.2  194.    10     0    30    10
##  5   187    80  3600  1750  31.5  34.4  170.     0     0    30    20
##  6   181    60  2250  1050  27.6  34.6  202.    10     0    30    10
##  7   143    80  3600  2450  32.1  35.7  158.     0     0    30    40
##  8   244    40  1467   620  36.9  31.9  200     10     0    40    20
##  9   228    40  1408   950  39.2  31.5  229     10     0    40    20
## 10   192    60  1676  1230  39.2  34.4  183     10     0    40    40
## # ℹ 22 more rows

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 it into a list by of frames
    split(.$cyl) %>%
    
    # Repeat the same operation over each element
    map(~lm(formula = mpg ~ wt, data = .x)) %>%
    
    # Return regression coefficients in a tidy tibble
    map(broom::tidy, conf.int = TRUE) %>%
    
    # Bind multiple data frames by row
    bind_rows(.id = "cyl") %>%
    
    # Filter for coefficient of interest
    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_errorbarh(aes(xmin=conf.low, xmax = conf.high)) 

Create your own

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

Import data

# excel filer
nhl_rosters <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2024/2024-01-09/nhl_rosters.csv')
## Rows: 54883 Columns: 18
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (10): team_code, position_type, headshot, first_name, last_name, positi...
## dbl   (7): season, player_id, sweater_number, height_in_inches, weight_in_po...
## date  (1): birth_date
## 
## ℹ 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.
nhl_rosters <- as_tibble(nhl_rosters)
nhl_rosters %>% lm(formula = height_in_centimeters ~ weight_in_kilograms, data = .)
## 
## Call:
## lm(formula = height_in_centimeters ~ weight_in_kilograms, data = .)
## 
## Coefficients:
##         (Intercept)  weight_in_kilograms  
##            135.4973               0.5457
nhl_rosters %>% distinct(team_code)
## # A tibble: 58 × 1
##    team_code
##    <chr>    
##  1 ATL      
##  2 HFD      
##  3 MNS      
##  4 QUE      
##  5 WIN      
##  6 CLR      
##  7 SEN      
##  8 HAM      
##  9 PIR      
## 10 QUA      
## # ℹ 48 more rows
reg_coeff_tbl <- nhl_rosters%>% 
    
    # Split it into a list by of frames
    split(.$team_code) %>%
    
    # Repeat the same operation over each element
    map(~lm(formula = height_in_centimeters ~ weight_in_kilograms, data = .x)) %>%
    
    # Return regression coefficients in a tidy tibble
    map(broom::tidy, conf.int = TRUE) %>%
    
    # Bind multiple data frames by row
    bind_rows(.id = "team_code") %>%
    
    # Filter for coefficient of interest
    filter(term == "weight_in_kilograms")
# Plot the regression coefficients
reg_coeff_tbl %>%
    mutate(estimate = -estimate,
           conf.low = -conf.low,
           conf.high = -conf.high) %>%
    ggplot(aes(x = estimate, y = team_code)) +
    geom_point() +
    geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
    labs(
        title = "Regression Coefficients by Team",
        x = "Coefficient Estimate (Inverse)",
        y = "Team Code") +
    theme_minimal()