Import your data

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(.f = 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 %>% 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 values in columns
double_by_vector <- function(x, factor) {x * factor}
10 %>% double_by_vector(factor = 5)
## [1] 50
100 %>% double_by_vector(factor = 5)
## [1] 500
mtcars %>% map_dfr(.x = ., .f = ~double_by_vector(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_vector, 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)
##                   cyl
## Mazda RX4           6
## Datsun 710          4
## Hornet Sportabout   8
reg_coeff_tbl <- mtcars %>% 
    
    # Split it into a list of dataf rames
    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 or 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

data <- read_excel("../00_data/myData.xlsx")
data
## # A tibble: 1,232 × 9
##    Column1 state condition                measure_id measure_name score footnote
##      <dbl> <chr> <chr>                    <chr>      <chr>        <chr> <chr>   
##  1       1 AK    Healthcare Personnel Va… HCP_COVID… Percentage … 7.3   NA      
##  2       2 AK    Healthcare Personnel Va… IMM_3      Healthcare … 80    NA      
##  3       3 AK    Emergency Department     OP_18b     Average (me… 140   25, 26  
##  4       4 AK    Emergency Department     OP_18b_HI… Average tim… 157   25, 26  
##  5       5 AK    Emergency Department     OP_18b_LO… Average tim… 136   25, 26  
##  6       6 AK    Emergency Department     OP_18b_ME… Average tim… 136   25, 26  
##  7       7 AK    Emergency Department     OP_18b_VE… Average tim… NA    25, 26  
##  8       8 AK    Emergency Department     OP_18c     Average (me… 196   25      
##  9       9 AK    Emergency Department     OP_18c_HI… Average tim… 230   25      
## 10      10 AK    Emergency Department     OP_18c_LO… Average tim… 182   25      
## # ℹ 1,222 more rows
## # ℹ 2 more variables: start_date <dttm>, end_date <dttm>

Own example

data <- na.omit(data[, c("state", "score", "condition")])
data
## # A tibble: 1,232 × 3
##    state score condition                       
##    <chr> <chr> <chr>                           
##  1 AK    7.3   Healthcare Personnel Vaccination
##  2 AK    80    Healthcare Personnel Vaccination
##  3 AK    140   Emergency Department            
##  4 AK    157   Emergency Department            
##  5 AK    136   Emergency Department            
##  6 AK    136   Emergency Department            
##  7 AK    NA    Emergency Department            
##  8 AK    196   Emergency Department            
##  9 AK    230   Emergency Department            
## 10 AK    182   Emergency Department            
## # ℹ 1,222 more rows

Mean of score with confidence levels

filtered_data <- data %>%
  filter(state %in% c("AK", "AZ", "CA"))

# Step 2: Select only numeric columns for calculations
numeric_data <- filtered_data %>%
  select(state, where(is.numeric))  # keeps 'state' and numeric columns

# Step 3: Calculate sum and standard deviation by state
standard_deviation <- numeric_data %>%
  group_by(state) %>%
  summarise(across(
    .cols = where(is.numeric),     # apply to all numeric columns
    .fns = list(
      total = ~sum(.x, na.rm = TRUE),
      stddev = ~sd(.x, na.rm = TRUE)
    ),
    .names = "{col}_{fn}"           # output column names like score_total, score_stddev
  ))

# Step 4: View the results
print(standard_deviation)
## # A tibble: 3 × 1
##   state
##   <chr>
## 1 AK   
## 2 AZ   
## 3 CA