data("mtcars")
mtcars <- as_tibble(mtcars)
# csv file
jobs_gender <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-03-05/jobs_gender.csv")
## Rows: 2088 Columns: 12
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): occupation, major_category, minor_category
## dbl (9): year, total_workers, workers_male, workers_female, percent_female, ...
##
## ℹ 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.
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)
## # A tibble: 32 × 1
## mpg
## <dbl>
## 1 21
## 2 21
## 3 22.8
## 4 21.4
## 5 18.7
## 6 18.1
## 7 14.3
## 8 24.4
## 9 22.8
## 10 19.2
## # … with 22 more rows
mtcars %>% select(mpg)
## # A tibble: 32 × 1
## mpg
## <dbl>
## 1 21
## 2 21
## 3 22.8
## 4 21.4
## 5 18.7
## 6 18.1
## 7 14.3
## 8 24.4
## 9 22.8
## 10 19.2
## # … with 22 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
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
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
## # … with 22 more rows
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)
## # A tibble: 3 × 1
## cyl
## <dbl>
## 1 6
## 2 4
## 3 8
reg_coeff_tbl <- mtcars %>%
# Split it into a list of data frames
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 for 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))
Choose either one of the two cases above and apply it to your data
jobs_gender %>% 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
## year occupation major_category
## 2014.50000 NA NA
## minor_category total_workers workers_male
## NA 196054.86638 111515.35536
## workers_female percent_female total_earnings
## 84539.51102 35.99971 49762.09339
## total_earnings_male total_earnings_female wage_percent_of_male
## NA NA NA
jobs_gender %>% 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
## year occupation major_category
## 2014.50000 NA NA
## minor_category total_workers workers_male
## NA 196054.86638 111515.35536
## workers_female percent_female total_earnings
## 84539.51102 35.99971 49762.09339
## total_earnings_male total_earnings_female wage_percent_of_male
## NA NA NA
jobs_gender %>% 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
## year occupation major_category
## 2014.50000 NA NA
## minor_category total_workers workers_male
## NA 196054.86638 111515.35536
## workers_female percent_female total_earnings
## 84539.51102 35.99971 49762.09339
## total_earnings_male total_earnings_female wage_percent_of_male
## NA NA NA
# Adding an argument
jobs_gender %>% map_dbl(.x = ., .f = ~mean(x = .x, trim = 500))
## Warning in mean.default(x = .x, trim = 500): argument is not numeric or logical:
## returning NA
## Warning in mean.default(x = .x, trim = 500): argument is not numeric or logical:
## returning NA
## Warning in mean.default(x = .x, trim = 500): argument is not numeric or logical:
## returning NA
## year occupation major_category
## 2014.5 NA NA
## minor_category total_workers workers_male
## NA 58997.0 32301.5
## workers_female percent_female total_earnings
## 15238.5 32.4 44437.0
## total_earnings_male total_earnings_female wage_percent_of_male
## NA NA NA
jobs_gender %>% map_dbl(mean, trim = 500)
## 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
## year occupation major_category
## 2014.5 NA NA
## minor_category total_workers workers_male
## NA 58997.0 32301.5
## workers_female percent_female total_earnings
## 15238.5 32.4 44437.0
## total_earnings_male total_earnings_female wage_percent_of_male
## NA NA NA
jobs_gender %>% select(.data = ., total_earnings)
## # A tibble: 2,088 × 1
## total_earnings
## <dbl>
## 1 120254
## 2 73557
## 3 67155
## 4 61371
## 5 78455
## 6 74114
## 7 62187
## 8 99167
## 9 70456
## 10 71927
## # … with 2,078 more rows
jobs_gender %>% select(total_earnings)
## # A tibble: 2,088 × 1
## total_earnings
## <dbl>
## 1 120254
## 2 73557
## 3 67155
## 4 61371
## 5 78455
## 6 74114
## 7 62187
## 8 99167
## 9 70456
## 10 71927
## # … with 2,078 more rows
# Double Value in the Columns
double_by_factor <- function(x, factor) {x * factor}
10 %>% double_by_factor(factor = 2)
## [1] 20
jobs_gender %>% select(where(is.numeric)) %>% map_dfr(.x = ., .f = ~double_by_factor(x = .x, factor = 10))
## # A tibble: 2,088 × 9
## year total_workers workers…¹ worke…² perce…³ total…⁴ total…⁵ total…⁶ wage_…⁷
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 20130 10242590 7824000 2418590 236 1202540 1261420 959210 760.
## 2 20130 9772840 6816270 2956570 303 735570 810410 607590 750.
## 3 20130 148150 83750 64400 435 671550 715300 653250 913.
## 4 20130 430150 177750 252400 587 613710 751900 558600 743.
## 5 20130 7545140 4400780 3144360 417 784550 919980 650400 707.
## 6 20130 441980 161410 280570 635 741140 900710 660520 733.
## 7 20130 1097030 728730 368300 336 621870 665790 550790 827.
## 8 20130 4890480 3543690 1346790 275 991670 1013180 909400 898.
## 9 20130 9906110 4608420 5297690 535 704560 902780 574060 636.
## 10 20130 146560 33870 112690 769 719270 975520 682070 NA
## # … with 2,078 more rows, and abbreviated variable names ¹workers_male,
## # ²workers_female, ³percent_female, ⁴total_earnings, ⁵total_earnings_male,
## # ⁶total_earnings_female, ⁷wage_percent_of_male
jobs_gender %>% select(where(is.numeric)) %>% map_dfr(double_by_factor, factor = 2)
## # A tibble: 2,088 × 9
## year total_workers workers…¹ worke…² perce…³ total…⁴ total…⁵ total…⁶ wage_…⁷
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 4026 2048518 1564800 483718 47.2 240508 252284 191842 152.
## 2 4026 1954568 1363254 591314 60.6 147114 162082 121518 150.
## 3 4026 29630 16750 12880 87 134310 143060 130650 183.
## 4 4026 86030 35550 50480 117. 122742 150380 111720 149.
## 5 4026 1509028 880156 628872 83.4 156910 183996 130080 141.
## 6 4026 88396 32282 56114 127 148228 180142 132104 147.
## 7 4026 219406 145746 73660 67.2 124374 133158 110158 165.
## 8 4026 978096 708738 269358 55 198334 202636 181880 180.
## 9 4026 1981222 921684 1059538 107 140912 180556 114812 127.
## 10 4026 29312 6774 22538 154. 143854 195104 136414 NA
## # … with 2,078 more rows, and abbreviated variable names ¹workers_male,
## # ²workers_female, ³percent_female, ⁴total_earnings, ⁵total_earnings_male,
## # ⁶total_earnings_female, ⁷wage_percent_of_male