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(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 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_by_factor <- function(x, factor) {x * factor}
10 %>% double_by_factor(factor = 2)
## [1] 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
## # … with 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

re_coeff_tbl <- mtcars %>% split(.$cyl) %>% map(~lm(formula = mpg ~ wt, data = .x)) %>% map(broom::tidy, conf.int = TRUE) %>% bind_rows(id. = “cyl”) %>% filter(term == “wt”)

re_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, xmox = conf.high))

COULD NOT USE bind_rows with one varibale ! Could not get function to knit. I tried everythng explained in video. Do you see a problem in my code?

Create your own

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

data <- read_excel("Raisin_Dataset.xlsx")
data
## # A tibble: 900 × 7
##     Area MajorAxisLength MinorAxisLength Eccentricity ConvexArea Extent Perime…¹
##    <dbl>           <dbl>           <dbl>        <dbl>      <dbl>  <dbl>    <dbl>
##  1 87524            442.            253.        0.820      90546  0.759    1184.
##  2 75166            407.            243.        0.802      78789  0.684    1122.
##  3 90856            442.            266.        0.798      93717  0.638    1209.
##  4 45928            287.            209.        0.685      47336  0.700     844.
##  5 79408            352.            291.        0.564      81463  0.793    1073.
##  6 49242            318.            200.        0.777      51368  0.658     882.
##  7 42492            310.            176.        0.823      43904  0.666     824.
##  8 60952            332.            235.        0.706      62329  0.744     933.
##  9 42256            323.            173.        0.845      44743  0.698     850.
## 10 64380            367.            228.        0.784      66125  0.664     982.
## # … with 890 more rows, and abbreviated variable name ¹​Perimeter
data %>% map_dbl(.x = ., .f = ~mean(x = .x))
##            Area MajorAxisLength MinorAxisLength    Eccentricity      ConvexArea 
##    8.780413e+04    4.309300e+02    2.544881e+02    7.815422e-01    9.118609e+04 
##          Extent       Perimeter 
##    6.995079e-01    1.165907e+03
data %>% map_dbl(.f = ~mean(x = .x))
##            Area MajorAxisLength MinorAxisLength    Eccentricity      ConvexArea 
##    8.780413e+04    4.309300e+02    2.544881e+02    7.815422e-01    9.118609e+04 
##          Extent       Perimeter 
##    6.995079e-01    1.165907e+03
data %>% map_dbl(mean)
##            Area MajorAxisLength MinorAxisLength    Eccentricity      ConvexArea 
##    8.780413e+04    4.309300e+02    2.544881e+02    7.815422e-01    9.118609e+04 
##          Extent       Perimeter 
##    6.995079e-01    1.165907e+03
#adding argument
data %>% map_dbl(.x = ., .f = ~mean(x = .x, trim = 1))
##            Area MajorAxisLength MinorAxisLength    Eccentricity      ConvexArea 
##    78902.000000      407.803951      247.848409        0.798846    81651.000000 
##          Extent       Perimeter 
##        0.707367     1119.509000
data %>% map_dbl(mean, trim = 1)
##            Area MajorAxisLength MinorAxisLength    Eccentricity      ConvexArea 
##    78902.000000      407.803951      247.848409        0.798846    81651.000000 
##          Extent       Perimeter 
##        0.707367     1119.509000
data %>% select(.data = ., Area)
## # A tibble: 900 × 1
##     Area
##    <dbl>
##  1 87524
##  2 75166
##  3 90856
##  4 45928
##  5 79408
##  6 49242
##  7 42492
##  8 60952
##  9 42256
## 10 64380
## # … with 890 more rows
data %>% select(Area)
## # A tibble: 900 × 1
##     Area
##    <dbl>
##  1 87524
##  2 75166
##  3 90856
##  4 45928
##  5 79408
##  6 49242
##  7 42492
##  8 60952
##  9 42256
## 10 64380
## # … with 890 more rows

Create your own function

double_by_factor <- function(x, factor) {x * factor}
10 %>% double_by_factor(factor = 100)
## [1] 1000
data %>% map_dfr(.x = ., .f = ~double_by_factor(x = .x, factor = 100))
## # A tibble: 900 × 7
##       Area MajorAxisLength MinorAxisLength Eccentricity ConvexA…¹ Extent Perim…²
##      <dbl>           <dbl>           <dbl>        <dbl>     <dbl>  <dbl>   <dbl>
##  1 8752400          44225.          25329.         82.0   9054600   75.9 118404 
##  2 7516600          40669.          24303.         80.2   7878900   68.4 112179.
##  3 9085600          44227.          26633.         79.8   9371700   63.8 120858.
##  4 4592800          28654.          20876.         68.5   4733600   70.0  84416.
##  5 7940800          35219.          29083.         56.4   8146300   79.3 107325.
##  6 4924200          31813.          20012.         77.7   5136800   65.8  88184.
##  7 4249200          31015.          17613.         82.3   4390400   66.6  82380.
##  8 6095200          33246.          23543.         70.6   6232900   74.4  93337.
##  9 4225600          32319.          17258.         84.5   4474300   69.8  84973.
## 10 6438000          36696.          22777.         78.4   6612500   66.4  98154.
## # … with 890 more rows, and abbreviated variable names ¹​ConvexArea, ²​Perimeter