Import your data

penguins <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2025/2025-04-15/penguins.csv') 
## Rows: 344 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): species, island, sex
## dbl (5): bill_len, bill_dep, flipper_len, body_mass, year
## 
## ℹ 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("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)
##                      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  value in column 

double_by_factor <- function(x, factor) 
    10 %>% double_by_factor(factor = 2)

 # mtcars %>% map(.x = ., .f = ~double_by_factor(x = .x, factor = 10))
# mtcars %>% map_dfr(double_by_factor, factor = 10)

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 it into a list of data frams
    split(.$cyl) %>%
    
    # repeat regression over each data group 
    map(~lm(formula = mpg ~wt, data = .x)) %>%
    

# extract coefficients fron 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

penguins %>% map_dbl(.x = ., .f = ~mean(x = .x))
## Warning in mean.default(x = .x): argument is not numeric or logical: returning
## NA
## Warning in mean.default(x = .x): argument is not numeric or logical: returning
## NA
## Warning in mean.default(x = .x): argument is not numeric or logical: returning
## NA
##     species      island    bill_len    bill_dep flipper_len   body_mass 
##          NA          NA          NA          NA          NA          NA 
##         sex        year 
##          NA    2008.029
penguins %>% map_dbl(.f = ~mean(x = .x))
## Warning in mean.default(x = .x): argument is not numeric or logical: returning
## NA
## Warning in mean.default(x = .x): argument is not numeric or logical: returning
## NA
## Warning in mean.default(x = .x): argument is not numeric or logical: returning
## NA
##     species      island    bill_len    bill_dep flipper_len   body_mass 
##          NA          NA          NA          NA          NA          NA 
##         sex        year 
##          NA    2008.029
penguins %>% map_dbl(mean)
## Warning in mean.default(.x[[i]], ...): argument is not numeric or logical:
## returning NA
## Warning in mean.default(.x[[i]], ...): argument is not numeric or logical:
## returning NA
## Warning in mean.default(.x[[i]], ...): argument is not numeric or logical:
## returning NA
##     species      island    bill_len    bill_dep flipper_len   body_mass 
##          NA          NA          NA          NA          NA          NA 
##         sex        year 
##          NA    2008.029
# Adding an argument 
penguins %>%  map_dbl(.x = ., .f = ~mean(x = .x, trim = 0.1))
## Warning in mean.default(x = .x, trim = 0.1): argument is not numeric or
## logical: returning NA
## Warning in mean.default(x = .x, trim = 0.1): argument is not numeric or
## logical: returning NA
## Warning in mean.default(x = .x, trim = 0.1): argument is not numeric or
## logical: returning NA
##     species      island    bill_len    bill_dep flipper_len   body_mass 
##          NA          NA          NA          NA          NA          NA 
##         sex        year 
##          NA    2008.036
penguins %>% map_dbl(mean, trim = 0.1)
## Warning in mean.default(.x[[i]], ...): argument is not numeric or logical:
## returning NA
## Warning in mean.default(.x[[i]], ...): argument is not numeric or logical:
## returning NA
## Warning in mean.default(.x[[i]], ...): argument is not numeric or logical:
## returning NA
##     species      island    bill_len    bill_dep flipper_len   body_mass 
##          NA          NA          NA          NA          NA          NA 
##         sex        year 
##          NA    2008.036
penguins %>% select(.data = ., species)
## # A tibble: 344 × 1
##    species
##    <chr>  
##  1 Adelie 
##  2 Adelie 
##  3 Adelie 
##  4 Adelie 
##  5 Adelie 
##  6 Adelie 
##  7 Adelie 
##  8 Adelie 
##  9 Adelie 
## 10 Adelie 
## # ℹ 334 more rows
penguins %>% select(species)
## # A tibble: 344 × 1
##    species
##    <chr>  
##  1 Adelie 
##  2 Adelie 
##  3 Adelie 
##  4 Adelie 
##  5 Adelie 
##  6 Adelie 
##  7 Adelie 
##  8 Adelie 
##  9 Adelie 
## 10 Adelie 
## # ℹ 334 more rows
# Double  value in column 

double_by_factor <- function(x, factor)
10 %>% double_by_factor(factor = 2)


# penguins %>% map_dfr(double_by_factor, factor = 10)