Skip to content

Instantly share code, notes, and snippets.

@alistaire47
Created May 2, 2022 05:09
Show Gist options
  • Save alistaire47/2e091dc0eba00761dd7f0de69c4fc4c3 to your computer and use it in GitHub Desktop.
Save alistaire47/2e091dc0eba00761dd7f0de69c4fc4c3 to your computer and use it in GitHub Desktop.
`purrr::pmap()` but with `Map()`-like semantics
pMap <- function(.l, .f, ...) {
.f <- rlang::as_function(.f, ...)
if (rlang::is_named(.l)) {
fun <- function(...) {
rlang::fn_env(.f) <- rlang::env(...)
.f(...)
}
} else {
fun <- .f
}
rlang::exec(Map, fun, !!!.l)
}
pMap_dbl <- function(.l, .f, ...) {
out <- pMap(.l, .f, ...)
purrr::simplify(out, .type = "double")
}
pMap_dbl(mtcars, max) # ok
#> [1] 160.0 160.0 108.0 258.0 360.0 225.0 360.0 146.7 140.8 167.6 167.6 275.8
#> [13] 275.8 275.8 472.0 460.0 440.0 78.7 75.7 71.1 120.1 318.0 304.0 350.0
#> [25] 400.0 79.0 120.3 113.0 351.0 175.0 335.0 121.0
pMap_dbl(mtcars, ~mean(c(...))) # nice, but same as `pmap_dbl()`
#> [1] 29.90727 29.98136 23.59818 38.73955 53.66455 35.04909 59.72000 24.63455
#> [9] 27.23364 31.86000 31.78727 46.43091 46.50000 46.35000 66.23273 66.05855
#> [17] 65.97227 19.44091 17.74227 18.81409 24.88864 47.24091 46.00773 58.75273
#> [25] 57.37955 18.92864 24.77909 24.88027 60.97182 34.50818 63.15545 26.26273
pMap_dbl(mtcars, ~mean(c(mpg, qsec))) # BUT you can use names as params too!
#> [1] 18.730 19.010 20.705 20.420 17.860 19.160 15.070 22.200 22.850 18.750
#> [11] 18.350 16.900 17.450 16.600 14.190 14.110 16.060 25.935 24.460 26.900
#> [21] 20.755 16.185 16.250 14.355 18.125 23.100 21.350 23.650 15.150 17.600
#> [31] 14.800 20.000
library(dplyr, warn.conflicts = FALSE)
data('mpg', package = 'ggplot2')
mpg %>% transmute(
mileage1 = rowMeans(cbind(cty, hwy)), # target
mileage2 = pMap_dbl(lst(cty, hwy), ~mean(c(...))), # named list, fun uses other param names
mileage3 = pMap_dbl(lst(cty, hwy), ~mean(c(cty, hwy))), # named list, fun uses list names as param names
mileage4 = pMap_dbl(., ~mean(c(cty, hwy))), # dataframe, fun uses col names as param names
mileage5 = pMap_dbl(list(cty, hwy), ~mean(c(...))) # unnamed list, fun uses other param names
)
#> # A tibble: 234 × 5
#> mileage1 mileage2 mileage3 mileage4 mileage5
#> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 23.5 23.5 23.5 23.5 23.5
#> 2 25 25 25 25 25
#> 3 25.5 25.5 25.5 25.5 25.5
#> 4 25.5 25.5 25.5 25.5 25.5
#> 5 21 21 21 21 21
#> 6 22 22 22 22 22
#> 7 22.5 22.5 22.5 22.5 22.5
#> 8 22 22 22 22 22
#> 9 20.5 20.5 20.5 20.5 20.5
#> 10 24 24 24 24 24
#> # … with 224 more rows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment