data("mtcars")
Case of numeric variables
mtcars %>%
# mutate(char_var = "A") %>%
map(mean) # map(.x = ., .f = mean)
## $mpg
## [1] 20.09062
##
## $cyl
## [1] 6.1875
##
## $disp
## [1] 230.7219
##
## $hp
## [1] 146.6875
##
## $drat
## [1] 3.596563
##
## $wt
## [1] 3.21725
##
## $qsec
## [1] 17.84875
##
## $vs
## [1] 0.4375
##
## $am
## [1] 0.40625
##
## $gear
## [1] 3.6875
##
## $carb
## [1] 2.8125
Create your own function
muliply_by_factor <- function(x, factor) {x * factor}
10 %>% muliply_by_factor(factor = 2)
## [1] 20
mtcars %>% map(.x = ., .f = ~muliply_by_factor(x = .x, factor = 2))
## $mpg
## [1] 42.0 42.0 45.6 42.8 37.4 36.2 28.6 48.8 45.6 38.4 35.6 32.8 34.6 30.4 20.8
## [16] 20.8 29.4 64.8 60.8 67.8 43.0 31.0 30.4 26.6 38.4 54.6 52.0 60.8 31.6 39.4
## [31] 30.0 42.8
##
## $cyl
## [1] 12 12 8 12 16 12 16 8 8 12 12 16 16 16 16 16 16 8 8 8 8 16 16 16 16
## [26] 8 8 8 16 12 16 8
##
## $disp
## [1] 320.0 320.0 216.0 516.0 720.0 450.0 720.0 293.4 281.6 335.2 335.2 551.6
## [13] 551.6 551.6 944.0 920.0 880.0 157.4 151.4 142.2 240.2 636.0 608.0 700.0
## [25] 800.0 158.0 240.6 190.2 702.0 290.0 602.0 242.0
##
## $hp
## [1] 220 220 186 220 350 210 490 124 190 246 246 360 360 360 410 430 460 132 104
## [20] 130 194 300 300 490 350 132 182 226 528 350 670 218
##
## $drat
## [1] 7.80 7.80 7.70 6.16 6.30 5.52 6.42 7.38 7.84 7.84 7.84 6.14 6.14 6.14 5.86
## [16] 6.00 6.46 8.16 9.86 8.44 7.40 5.52 6.30 7.46 6.16 8.16 8.86 7.54 8.44 7.24
## [31] 7.08 8.22
##
## $wt
## [1] 5.240 5.750 4.640 6.430 6.880 6.920 7.140 6.380 6.300 6.880
## [11] 6.880 8.140 7.460 7.560 10.500 10.848 10.690 4.400 3.230 3.670
## [21] 4.930 7.040 6.870 7.680 7.690 3.870 4.280 3.026 6.340 5.540
## [31] 7.140 5.560
##
## $qsec
## [1] 32.92 34.04 37.22 38.88 34.04 40.44 31.68 40.00 45.80 36.60 37.80 34.80
## [13] 35.20 36.00 35.96 35.64 34.84 38.94 37.04 39.80 40.02 33.74 34.60 30.82
## [25] 34.10 37.80 33.40 33.80 29.00 31.00 29.20 37.20
##
## $vs
## [1] 0 0 2 2 0 2 0 2 2 2 2 0 0 0 0 0 0 2 2 2 2 0 0 0 0 2 0 2 0 0 0 2
##
## $am
## [1] 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 2 0 0 0 0 0 2 2 2 2 2 2 2
##
## $gear
## [1] 8 8 8 6 6 6 6 8 8 8 8 6 6 6 6 6 6 8 8 8 6 6 6 6 6
## [26] 8 10 10 10 10 10 8
##
## $carb
## [1] 8 8 2 2 4 2 8 4 4 8 8 6 6 6 8 8 8 2 4 2 2 4 4 8 4
## [26] 2 4 4 8 12 16 4
mtcars %>% map_dfc(.x = ., .f = ~muliply_by_factor(x = .x, factor = 2))
## # 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 42 12 320 220 7.8 5.24 32.9 0 2 8 8
## 2 42 12 320 220 7.8 5.75 34.0 0 2 8 8
## 3 45.6 8 216 186 7.7 4.64 37.2 2 2 8 2
## 4 42.8 12 516 220 6.16 6.43 38.9 2 0 6 2
## 5 37.4 16 720 350 6.3 6.88 34.0 0 0 6 4
## 6 36.2 12 450 210 5.52 6.92 40.4 2 0 6 2
## 7 28.6 16 720 490 6.42 7.14 31.7 0 0 6 8
## 8 48.8 8 293. 124 7.38 6.38 40 2 0 8 4
## 9 45.6 8 282. 190 7.84 6.3 45.8 2 0 8 4
## 10 38.4 12 335. 246 7.84 6.88 36.6 2 0 8 8
## # … with 22 more rows
mtcars %>% map(muliply_by_factor, factor = 2)
## $mpg
## [1] 42.0 42.0 45.6 42.8 37.4 36.2 28.6 48.8 45.6 38.4 35.6 32.8 34.6 30.4 20.8
## [16] 20.8 29.4 64.8 60.8 67.8 43.0 31.0 30.4 26.6 38.4 54.6 52.0 60.8 31.6 39.4
## [31] 30.0 42.8
##
## $cyl
## [1] 12 12 8 12 16 12 16 8 8 12 12 16 16 16 16 16 16 8 8 8 8 16 16 16 16
## [26] 8 8 8 16 12 16 8
##
## $disp
## [1] 320.0 320.0 216.0 516.0 720.0 450.0 720.0 293.4 281.6 335.2 335.2 551.6
## [13] 551.6 551.6 944.0 920.0 880.0 157.4 151.4 142.2 240.2 636.0 608.0 700.0
## [25] 800.0 158.0 240.6 190.2 702.0 290.0 602.0 242.0
##
## $hp
## [1] 220 220 186 220 350 210 490 124 190 246 246 360 360 360 410 430 460 132 104
## [20] 130 194 300 300 490 350 132 182 226 528 350 670 218
##
## $drat
## [1] 7.80 7.80 7.70 6.16 6.30 5.52 6.42 7.38 7.84 7.84 7.84 6.14 6.14 6.14 5.86
## [16] 6.00 6.46 8.16 9.86 8.44 7.40 5.52 6.30 7.46 6.16 8.16 8.86 7.54 8.44 7.24
## [31] 7.08 8.22
##
## $wt
## [1] 5.240 5.750 4.640 6.430 6.880 6.920 7.140 6.380 6.300 6.880
## [11] 6.880 8.140 7.460 7.560 10.500 10.848 10.690 4.400 3.230 3.670
## [21] 4.930 7.040 6.870 7.680 7.690 3.870 4.280 3.026 6.340 5.540
## [31] 7.140 5.560
##
## $qsec
## [1] 32.92 34.04 37.22 38.88 34.04 40.44 31.68 40.00 45.80 36.60 37.80 34.80
## [13] 35.20 36.00 35.96 35.64 34.84 38.94 37.04 39.80 40.02 33.74 34.60 30.82
## [25] 34.10 37.80 33.40 33.80 29.00 31.00 29.20 37.20
##
## $vs
## [1] 0 0 2 2 0 2 0 2 2 2 2 0 0 0 0 0 0 2 2 2 2 0 0 0 0 2 0 2 0 0 0 2
##
## $am
## [1] 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 2 0 0 0 0 0 2 2 2 2 2 2 2
##
## $gear
## [1] 8 8 8 6 6 6 6 8 8 8 8 6 6 6 6 6 6 8 8 8 6 6 6 6 6
## [26] 8 10 10 10 10 10 8
##
## $carb
## [1] 8 8 2 2 4 2 8 4 4 8 8 6 6 6 8 8 8 2 4 2 2 4 4 8 4
## [26] 2 4 4 8 12 16 4
When you have a grouping variable (factor)
Choose either one of the two cases above and apply it to your data
results <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-09-07/results.csv')
## Warning: One or more parsing issues, see `problems()` for details
## Rows: 25220 Columns: 18
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (8): position, positionText, time, milliseconds, fastestLap, rank, fast...
## dbl (10): resultId, raceId, driverId, constructorId, number, grid, positionO...
##
## ℹ 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.
skimr::skim(results)
| Name | results |
| Number of rows | 25220 |
| Number of columns | 18 |
| _______________________ | |
| Column type frequency: | |
| character | 8 |
| numeric | 10 |
| ________________________ | |
| Group variables | None |
Variable type: character
| skim_variable | n_missing | complete_rate | min | max | empty | n_unique | whitespace |
|---|---|---|---|---|---|---|---|
| position | 0 | 1 | 1 | 2 | 0 | 34 | 0 |
| positionText | 0 | 1 | 1 | 2 | 0 | 39 | 0 |
| time | 0 | 1 | 2 | 11 | 0 | 6488 | 0 |
| milliseconds | 0 | 1 | 2 | 8 | 0 | 6687 | 0 |
| fastestLap | 0 | 1 | 1 | 2 | 0 | 80 | 0 |
| rank | 0 | 1 | 1 | 2 | 0 | 26 | 0 |
| fastestLapTime | 0 | 1 | 2 | 8 | 0 | 6266 | 0 |
| fastestLapSpeed | 0 | 1 | 2 | 7 | 0 | 6395 | 0 |
Variable type: numeric
| skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
|---|---|---|---|---|---|---|---|---|---|---|
| resultId | 0 | 1 | 12611.23 | 7281.58 | 1 | 6305.75 | 12610.5 | 18915.25 | 25225 | ▇▇▇▇▇ |
| raceId | 0 | 1 | 517.95 | 290.34 | 1 | 287.00 | 503.0 | 762.00 | 1064 | ▆▇▇▆▆ |
| driverId | 0 | 1 | 250.84 | 258.25 | 1 | 56.00 | 158.0 | 347.00 | 854 | ▇▃▂▁▂ |
| constructorId | 0 | 1 | 47.48 | 58.39 | 1 | 6.00 | 25.0 | 57.00 | 214 | ▇▂▁▁▁ |
| number | 6 | 1 | 17.59 | 14.80 | 0 | 7.00 | 15.0 | 23.00 | 208 | ▇▁▁▁▁ |
| grid | 0 | 1 | 11.21 | 7.27 | 0 | 5.00 | 11.0 | 17.00 | 34 | ▇▇▇▃▁ |
| positionOrder | 0 | 1 | 12.93 | 7.74 | 1 | 6.00 | 12.0 | 19.00 | 39 | ▇▇▆▂▁ |
| points | 0 | 1 | 1.80 | 4.03 | 0 | 0.00 | 0.0 | 2.00 | 50 | ▇▁▁▁▁ |
| laps | 0 | 1 | 45.79 | 30.04 | 0 | 21.00 | 52.0 | 66.00 | 200 | ▅▇▁▁▁ |
| statusId | 0 | 1 | 17.72 | 26.10 | 1 | 1.00 | 11.0 | 14.00 | 139 | ▇▁▁▁▁ |
mean_results <- results %>%
map(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
## 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
## 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
mean_laps <- function(.data, laps) {
mean_results <- .data %>%
mean(laps) %>%
return(mean_laps)
}