Import your data

fires <- readxl::read_xlsx("myData.xlsx")
data <- as_tibble(fires)

fires2 = select(fires, -1, -5)

Repeat the same operation over different columns of a data frame

Case of numeric variables

fires2 %>% map_dbl(.x = ., .f = ~mean(x = .x))
##                  NUMBER_FIRES                  ACRES_BURNED 
##                  4.970614e+03                  1.919903e+05 
##                  DAMAGE_COSTS          Damage_Cost_Per_Acre 
##                  9.126746e+07                  4.094571e+02 
## Average_Acres_Burned_Per_Fire 
##                  5.251226e+01
fires2 %>% map_dbl(.f = ~mean(x = .x))
##                  NUMBER_FIRES                  ACRES_BURNED 
##                  4.970614e+03                  1.919903e+05 
##                  DAMAGE_COSTS          Damage_Cost_Per_Acre 
##                  9.126746e+07                  4.094571e+02 
## Average_Acres_Burned_Per_Fire 
##                  5.251226e+01
fires2 %>% map_dbl(mean)
##                  NUMBER_FIRES                  ACRES_BURNED 
##                  4.970614e+03                  1.919903e+05 
##                  DAMAGE_COSTS          Damage_Cost_Per_Acre 
##                  9.126746e+07                  4.094571e+02 
## Average_Acres_Burned_Per_Fire 
##                  5.251226e+01
# Adding an argument
fires2 %>% map_dbl(.x = ., .f = ~mean(x = .x, trim = 0.1))
##                  NUMBER_FIRES                  ACRES_BURNED 
##                  4.906881e+03                  1.646687e+05 
##                  DAMAGE_COSTS          Damage_Cost_Per_Acre 
##                  2.455526e+07                  1.882805e+02 
## Average_Acres_Burned_Per_Fire 
##                  4.027054e+01
fires2 %>% map_dbl(mean, trim = 0.1)
##                  NUMBER_FIRES                  ACRES_BURNED 
##                  4.906881e+03                  1.646687e+05 
##                  DAMAGE_COSTS          Damage_Cost_Per_Acre 
##                  2.455526e+07                  1.882805e+02 
## Average_Acres_Burned_Per_Fire 
##                  4.027054e+01
data %>% select(.data = ., ACRES_BURNED)
## # A tibble: 83 × 1
##    ACRES_BURNED
##           <dbl>
##  1       129210
##  2       363052
##  3       127262
##  4       756696
##  5        71312
##  6       221061
##  7       513620
##  8       156015
##  9       278599
## 10       573597
## # ℹ 73 more rows
data %>% select(ACRES_BURNED)
## # A tibble: 83 × 1
##    ACRES_BURNED
##           <dbl>
##  1       129210
##  2       363052
##  3       127262
##  4       756696
##  5        71312
##  6       221061
##  7       513620
##  8       156015
##  9       278599
## 10       573597
## # ℹ 73 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
fires2 %>% map(.x = ., .f = ~double_by_factor(x = .x, factor = 10))
## $NUMBER_FIRES
##  [1]  19940  23380  14470  38050  29070  41500  24910  44970  54600  52360
## [11]  21630  20340  25940  26430  24730  19730  26080  22640  21590  22630
## [21]  20800  20170  19410  17580  29360  30870  31670  28690  50050  53710
## [31]  46720  62620  58780  76380  45720  56960  58590  51700  60010  61670
## [41]  69840  85090  80290  81320  84960 100380  91590  74970  57090  59690
## [51]  78300  72380  71490  80630  81300  66350  72830  62380  79390  69880
## [61]  72070  66010  72370  68350  52270  75620  51770  62230  57590  59610
## [71]  55740  49080  48050  41110  40520  33910  31530  35550  34200  41520
## [81]  32150  35430  32330
## 
## $ACRES_BURNED
##  [1] 1292100 3630520 1272620 7566960  713120 2210610 5136200 1560150 2785990
## [10] 5735970 5533280 6488380 5099980 2348790 2564720 1332230 1322530 3033930
## [19] 1483600 1209740 1251500 1400720 1614880  646170 1243160 1461590 1476580
## [28] 1237430 3279510 1300980  372220 2195520 2531380 1193680 1631600 1210390
## [37]  791950 2084790  450080  647140 1176690  938980  939670 1239040  885860
## [46] 2470060 1602910 2098250  691580  707730 1036700 2232820  536310  869450
## [55] 1908350  736010 2121420  231540 1914900 1226060 1407920 1211980 2326240
## [64]  577880  924560 2852720  727180  909850 1228090 4043280 1681340  740040
## [73] 2228960 5302320 4397170 1070310  302340 1513570 1408180 1429240 1675320
## [82] 2976470 2509960
## 
## $DAMAGE_COSTS
##  [1]     3186360     5637100     1655430    18771470     1515840     4042250
##  [7]     8475790     2721780     5157370    14848640    15020270    13651260
## [13]    20859840     6222650    22516050     7175640     6051020    20250790
## [19]    11600980    11204600     6866770    13491580    67329340     7646120
## [25]     5471810    11257310    30120550    26340660    45385140    13470640
## [31]     5709670    64724190    51830490    80425470    64055320    25373300
## [37]    15544370   210324770    54358800    66970330    99587510    85380210
## [43]   108600950   416578010   267995160   304742670   175846330   550703000
## [49]   229395000   279226000   236567000   429811000   197619000   468149990
## [55]   649582200   273653130   957077490   269041930  1707750230   540777260
## [61]   420047810   324576020  1002831720   819194190   274313180  1342585340
## [67]   298768530   872950010  1739768600  9741868570  1267904170   493929430
## [73]   602703820  2541727350  8893083480   339568970    33974420  2545347040
## [79]   282132000   297997530   200341680 30618366660  1482668930
## 
## $Damage_Cost_Per_Acre
##  [1]     24.66032     15.52698     13.00805     24.80715     21.25645
##  [6]     18.28568     16.50206     17.44563     18.51180     25.88689
## [11]     27.14533     21.03955     40.90181     26.49300     87.79145
## [16]     53.86187     45.75337     66.74772     78.19480     92.61990
## [21]     54.86832     96.31889    416.93092    118.32985     44.01533
## [26]     77.02098    203.98861    212.86586    138.39000    103.54225
## [31]    153.39504    294.80119    204.75191    673.76072    392.59206
## [36]    209.62913    196.27969   1008.85351   1207.75862   1034.86618
## [41]    846.33599    909.28678   1155.73499   3362.10300   3025.25410
## [46]   1233.74602   1097.04431   2624.58239   3316.96984   3945.37465
## [51]   2281.92341   1924.96932   3684.79051   5384.43832   3403.89446
## [56]   3718.06266   4511.49461  11619.67392   8918.22147   4410.69165
## [61]   2983.46362   2678.06416   4310.95553  14175.85295   2966.95920
## [66]   4706.33410   4108.59113   9594.43875  14166.45848  24093.97462
## [71]   7541.03376   6674.36125   2703.96876   4793.61364  20224.56143
## [76]   3172.62260   1123.71568  16816.84389   2003.52228   2085.00693
## [81]   1195.84127 102868.05061   5907.14167
## 
## $Average_Acres_Burned_Per_Fire
##  [1]  647.99398 1552.83148  879.48860 1988.68857  245.31132  532.67711
##  [7] 2061.90285  346.93129  510.25458 1095.48701 2558.15072 3189.96067
## [13] 1966.06785  888.68331 1037.08856  675.23061  507.10506 1340.07509
## [19]  687.16999  534.57357  601.68269  694.45711  831.98351  367.55973
## [25]  423.41962  473.46615  466.23934  431.31056  655.24675  242.22305
## [31]   79.67038  350.61003  430.65328  156.28175  356.86789  212.49824
## [37]  135.16812  403.24758   75.00083  104.93595  168.48368  110.35139
## [43]  117.03450  152.36596  104.26789  246.07093  175.00928  279.87862
## [49]  121.13855  118.56760  132.40102  308.48577   75.01888  107.83207
## [55]  234.72940  110.92841  291.28381   37.11767  241.20166  175.45220
## [61]  195.35452  183.60551  321.43706   84.54718  176.88158  377.24412
## [67]  140.46359  146.20762  213.24709  678.28888  301.63976  150.78240
## [73]  463.88345 1289.78837 1085.18509  315.63256   95.88963  425.75809
## [79]  411.74854  344.22929  521.09487  840.09879  776.35633

Repeat the same operation over different elements of a list

When you have a grouping variable (factor)

fires2 %>% lm(formula = ACRES_BURNED ~ NUMBER_FIRES, data = .)
## 
## Call:
## lm(formula = ACRES_BURNED ~ NUMBER_FIRES, data = .)
## 
## Coefficients:
##  (Intercept)  NUMBER_FIRES  
##    274461.05        -16.59
fires %>% distinct(SEVERITY)
## # A tibble: 3 × 1
##   SEVERITY
##   <chr>   
## 1 Moderate
## 2 Severe  
## 3 Mild
reg_coeff_tbl <- fires %>%
    
    # Split it into a list of data frames
    split(.$SEVERITY) %>%
    
    # Repeat regression over each group
    map(.x = ., .f = ~lm(formula = ACRES_BURNED ~ NUMBER_FIRES, data = .)) %>%
    
    map(broom::tidy, conf.int = TRUE) %>%
    
    # Convert to tibble
    bind_rows(.id = "SEVERITY") %>%
    
    # Filter for 
    filter(term == "NUMBER_FIRES")
reg_coeff_tbl %>%
    
    mutate(estimate = -estimate,
           conf.low = -conf.low,
           conf.high = -conf.high) %>%
    
    ggplot(aes(x = estimate, y = SEVERITY)) +
    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

fires2 %>% map_dbl(.x = ., .f = ~mean(x = .x))
##                  NUMBER_FIRES                  ACRES_BURNED 
##                  4.970614e+03                  1.919903e+05 
##                  DAMAGE_COSTS          Damage_Cost_Per_Acre 
##                  9.126746e+07                  4.094571e+02 
## Average_Acres_Burned_Per_Fire 
##                  5.251226e+01
fires2 %>% map_dbl(.f = ~mean(x = .x))
##                  NUMBER_FIRES                  ACRES_BURNED 
##                  4.970614e+03                  1.919903e+05 
##                  DAMAGE_COSTS          Damage_Cost_Per_Acre 
##                  9.126746e+07                  4.094571e+02 
## Average_Acres_Burned_Per_Fire 
##                  5.251226e+01
fires2 %>% map_dbl(mean)
##                  NUMBER_FIRES                  ACRES_BURNED 
##                  4.970614e+03                  1.919903e+05 
##                  DAMAGE_COSTS          Damage_Cost_Per_Acre 
##                  9.126746e+07                  4.094571e+02 
## Average_Acres_Burned_Per_Fire 
##                  5.251226e+01
# Adding an argument
fires2 %>% map_dbl(.x = ., .f = ~mean(x = .x, trim = 0.2))
##                  NUMBER_FIRES                  ACRES_BURNED 
##                  4.897882e+03                  1.552624e+05 
##                  DAMAGE_COSTS          Damage_Cost_Per_Acre 
##                  1.740754e+07                  1.518660e+02 
## Average_Acres_Burned_Per_Fire 
##                  3.645779e+01
fires2 %>% map_dbl(mean, trim = 0.2)
##                  NUMBER_FIRES                  ACRES_BURNED 
##                  4.897882e+03                  1.552624e+05 
##                  DAMAGE_COSTS          Damage_Cost_Per_Acre 
##                  1.740754e+07                  1.518660e+02 
## Average_Acres_Burned_Per_Fire 
##                  3.645779e+01
fires2 %>% select(.data = ., NUMBER_FIRES)
## # A tibble: 83 × 1
##    NUMBER_FIRES
##           <dbl>
##  1         1994
##  2         2338
##  3         1447
##  4         3805
##  5         2907
##  6         4150
##  7         2491
##  8         4497
##  9         5460
## 10         5236
## # ℹ 73 more rows
fires2 %>% select(NUMBER_FIRES)
## # A tibble: 83 × 1
##    NUMBER_FIRES
##           <dbl>
##  1         1994
##  2         2338
##  3         1447
##  4         3805
##  5         2907
##  6         4150
##  7         2491
##  8         4497
##  9         5460
## 10         5236
## # ℹ 73 more rows