Import your data

salaries <- read_csv("../00_data/salaries.csv")
## Rows: 397 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): rank, discipline, sex
## dbl (3): yrs.since.phd, yrs.service, salary
## 
## ℹ 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.
salaries <- as_tibble(salaries)

Repeat the same operation over different columns of a data frame

Case of numeric variables

salaries %>% 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
##          rank    discipline yrs.since.phd   yrs.service           sex 
##            NA            NA      22.31486      17.61461            NA 
##        salary 
##  113706.45844
salaries %>% 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
##          rank    discipline yrs.since.phd   yrs.service           sex 
##            NA            NA      22.31486      17.61461            NA 
##        salary 
##  113706.45844
salaries %>% 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
##          rank    discipline yrs.since.phd   yrs.service           sex 
##            NA            NA      22.31486      17.61461            NA 
##        salary 
##  113706.45844
# Adding an argument
salaries %>% 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
##          rank    discipline yrs.since.phd   yrs.service           sex 
##            NA            NA      21.83386      16.50784            NA 
##        salary 
##  111401.60502
salaries %>% 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
##          rank    discipline yrs.since.phd   yrs.service           sex 
##            NA            NA      21.83386      16.50784            NA 
##        salary 
##  111401.60502
salaries %>% select(.data = ., salary)
## # A tibble: 397 × 1
##    salary
##     <dbl>
##  1 139750
##  2 173200
##  3  79750
##  4 115000
##  5 141500
##  6  97000
##  7 175000
##  8 147765
##  9 119250
## 10 129000
## # ℹ 387 more rows
salaries %>% select(salary)
## # A tibble: 397 × 1
##    salary
##     <dbl>
##  1 139750
##  2 173200
##  3  79750
##  4 115000
##  5 141500
##  6  97000
##  7 175000
##  8 147765
##  9 119250
## 10 129000
## # ℹ 387 more rows

Create your own function

# Double values in columns
double_by_factor <- function(x, factor) {x * factor}
10 %>% double_by_factor(factor = 2)
## [1] 20
salaries %>% select(yrs.service, salary) %>% map_dfr(.x = ., .f = ~double_by_factor(x = .x, factor = 10))
## # A tibble: 397 × 2
##    yrs.service  salary
##          <dbl>   <dbl>
##  1         180 1397500
##  2         160 1732000
##  3          30  797500
##  4         390 1150000
##  5         410 1415000
##  6          60  970000
##  7         230 1750000
##  8         450 1477650
##  9         200 1192500
## 10         180 1290000
## # ℹ 387 more rows
salaries %>% select(yrs.service, salary) %>% map_dfr(double_by_factor, factor = 10)
## # A tibble: 397 × 2
##    yrs.service  salary
##          <dbl>   <dbl>
##  1         180 1397500
##  2         160 1732000
##  3          30  797500
##  4         390 1150000
##  5         410 1415000
##  6          60  970000
##  7         230 1750000
##  8         450 1477650
##  9         200 1192500
## 10         180 1290000
## # ℹ 387 more rows

Repeat the same operation over different elements of a list

When you have a grouping variable (factor)

salaries %>% lm(formula = salary ~ sex, data = .)
## 
## Call:
## lm(formula = salary ~ sex, data = .)
## 
## Coefficients:
## (Intercept)      sexMale  
##      101002        14088
salaries %>% distinct(yrs.since.phd)
## # A tibble: 53 × 1
##    yrs.since.phd
##            <dbl>
##  1            19
##  2            20
##  3             4
##  4            45
##  5            40
##  6             6
##  7            30
##  8            21
##  9            18
## 10            12
## # ℹ 43 more rows
 reg_coeff_tbl <- salaries %>%
    
    # Split it into a list of data frames
    split(.$rank) %>%

    # Repeat regression over each group
    map(~lm(formula = salary ~ sex, data = .x)) %>%
    
    # Extract coefficients from regression results
    map(broom::tidy, conf.int = TRUE) %>%

    # Convert to tibble
    bind_rows(.id = "yrs.since.phd") %>%
    
    # Filter for wt coefficients
    filter(term =="sexMale")
reg_coeff_tbl %>%
    
    mutate(estimate = -estimate, 
           conf.low = - conf.low,
           conf.high = -conf.high) %>%
    
    ggplot(aes(x = -estimate, y = yrs.since.phd)) + 
    geom_point() +
    geom_errorbar(aes(xmin = conf.low, xmax = conf.high))

Create yor own

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