Import your data

data("mtcars")
myData <- read.csv("/Users/takeru/Desktop/PSU_DAT3000_IntroToDA/00_data/myData.csv")

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 values in columns
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
## # ℹ 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)
##                   cyl
## Mazda RX4           6
## Datsun 710          4
## Hornet Sportabout   8
reg_coeff_tbl <- mtcars %>%
    
    # Split it into a list of data frames
    split(.$cyl) %>%

    # Repeat regression over each group
    map(.x = ., .f = ~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 for wt coefficients
    filter(term == "wt")

reg_coeff_tbl
## # A tibble: 3 × 8
##   cyl   term  estimate std.error statistic p.value conf.low conf.high
##   <chr> <chr>    <dbl>     <dbl>     <dbl>   <dbl>    <dbl>     <dbl>
## 1 4     wt       -5.65     1.85      -3.05  0.0137    -9.83    -1.46 
## 2 6     wt       -2.78     1.33      -2.08  0.0918    -6.21     0.651
## 3 8     wt       -2.19     0.739     -2.97  0.0118    -3.80    -0.582
reg_coeff_tbl %>%
    mutate(estimate = -1 * 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

# This code checks if having more books written about a language increases the number of users differently between open-source and and closed-source programming languages.
library(dplyr)
library(purrr)
library(broom)
library(ggplot2)

# Create a numeric predictor (like book_count)
myData2 <- myData %>%
    group_by(language) %>%
    mutate(country_count = n_distinct(country)) %>%
    ungroup()

reg_tbl_languages <- myData2 %>%
    # Remove NA entries
    filter(!is.na(family)) %>%
    filter(!is.na(native_speakers)) %>%
    filter(!is.na(country_count))  %>%
    
    # Split into groups (like TRUE/FALSE before)
    split(.$family) %>%

    # Repeat regression over each group
    map(lm, formula = native_speakers ~ country_count) %>%
        
    # Extract coefficients
    map(broom::tidy, conf.int = TRUE) %>%
        
    # Convert to tibble
    bind_rows(.id = "family") %>%
    
    # Keep only slope
    filter(term == "country_count")
## Warning in summary.lm(x): essentially perfect fit: summary may be unreliable
## Warning in summary.lm(object, ...): essentially perfect fit: summary may be
## unreliable
## Warning in qt(a, object$df.residual): NaNs produced
## Warning in summary.lm(x): essentially perfect fit: summary may be unreliable
## Warning in summary.lm(object, ...): essentially perfect fit: summary may be
## unreliable
## Warning in qt(a, object$df.residual): NaNs produced
## Warning in qt(a, object$df.residual): NaNs produced
## Warning in qt(a, object$df.residual): NaNs produced
## Warning in summary.lm(x): essentially perfect fit: summary may be unreliable
## Warning in summary.lm(object, ...): essentially perfect fit: summary may be
## unreliable
reg_tbl_languages
## # A tibble: 17 × 8
##    family        term  estimate std.error statistic   p.value conf.low conf.high
##    <chr>         <chr>    <dbl>     <dbl>     <dbl>     <dbl>    <dbl>     <dbl>
##  1 Afro-Asiatic  coun…   3.29e5  6.64e+ 3   4.95e 1  1.81e- 5   3.07e5   349685.
##  2 Afroasiatic   coun…   1.37e7  5.05e+ 5   2.72e 1  3.61e-29   1.27e7 14755590.
##  3 Arabic-based  coun…  -2.  e5  3.56e-11  -5.61e15  1.13e-16  -2.00e5  -200000.
##  4 Austronesian  coun…  NA      NA         NA       NA         NA            NA 
##  5 English       coun…  NA      NA         NA       NA         NA            NA 
##  6 French        coun…  NA      NA         NA       NA         NA            NA 
##  7 Indo-European coun…   4.27e6  4.20e+ 5   1.02e 1  7.55e- 6   3.30e6  5233553.
##  8 Khoe–Kwadi    coun…  -1.62e4  1.14e+ 4  -1.42e 0  1.99e- 1  -4.31e4    10761.
##  9 Kongo-based   coun…  NA      NA         NA       NA         NA            NA 
## 10 Kxʼa          coun…   6.83e3  4.79e+ 3   1.43e 0  1.97e- 1  -4.50e3    18162.
## 11 Language      coun…  NA      NA         NA       NA         NA            NA 
## 12 Mande         coun…  NA      NA         NA       NA         NA            NA 
## 13 Niger–Congo   coun…   1.90e6  1.19e+ 5   1.60e 1  5.87e-48   1.66e6  2128782.
## 14 Nilo-Saharan  coun…   1.28e6  1.98e+ 5   6.47e 0  3.05e- 9   8.87e5  1671002.
## 15 Portuguese    coun…  NA      NA         NA       NA         NA            NA 
## 16 Tuu           coun…  NA      NA         NA       NA         NA            NA 
## 17 Ubangian      coun…   1.50e5  1.18e+ 5   1.28e 0  2.34e- 1  -1.16e5   416900.
reg_tbl_languages %>%
    ggplot(aes(x = estimate, y = family)) +
    geom_point() +
    geom_errorbar(aes(xmin = conf.low, xmax = conf.high))
## Warning: Removed 8 rows containing missing values or values outside the scale range
## (`geom_point()`).