assigning `pmap` output to dataframes with pattern

2019-09-05 04:22发布

问题:

I am using pmap to run the same function across multiple dataframes and wish to assign each element of the output list to a separate object with a name that has a pattern. But I can't figure out how to do this.

For example, here is a minimal example where I am computing quantiles for three different variables-

# function call 
purrr::pmap(.l = list(
  x = list(iris$Sepal.Length, mtcars$wt, anscombe$y4),
  probs = list(seq(0, 1, 0.10)),
  na.rm = list(TRUE)
),
.f = stats::quantile)

# output
#> [[1]]
#>   0%  10%  20%  30%  40%  50%  60%  70%  80%  90% 100% 
#> 4.30 4.80 5.00 5.27 5.60 5.80 6.10 6.30 6.52 6.90 7.90 
#> 
#> [[2]]
#>     0%    10%    20%    30%    40%    50%    60%    70%    80%    90% 
#> 1.5130 1.9555 2.3490 2.7730 3.1580 3.3250 3.4400 3.5550 3.7700 4.0475 
#>   100% 
#> 5.4240 
#> 
#> [[3]]
#>    0%   10%   20%   30%   40%   50%   60%   70%   80%   90%  100% 
#>  5.25  5.56  5.76  6.58  6.89  7.04  7.71  7.91  8.47  8.84 12.50

This produces a list of 3 elements (each of which is a dataframe). Instead of getting this list in return, I want to automatically assign each element to an object with a pattern name (e.g., [[1]] as df_1, [[2]] as df_2, [[3]] as df_3, etc.). (I know about the assign function, but I can't figure out how to combine it with purrr.)

回答1:

You can do it with map2:

library(purrr)
res <- pmap(.l = list(
    x = list(iris$Sepal.Length, mtcars$wt, anscombe$y4),
    probs = list(seq(0, 1, 0.10)),
    na.rm = list(TRUE)
), .f = stats::quantile)

map2(.x = paste0('df_', seq_along(res)), .y = res,
     .f = ~ assign(.x, .y, envir = .GlobalEnv))

# > df_1
#   0%  10%  20%  30%  40%  50%  60%  70%  80%  90% 100% 
# 4.30 4.80 5.00 5.27 5.60 5.80 6.10 6.30 6.52 6.90 7.90 
# > df_2
#     0%    10%    20%    30%    40%    50%    60%    70%    80%    90%   100% 
# 1.5130 1.9555 2.3490 2.7730 3.1580 3.3250 3.4400 3.5550 3.7700 4.0475 5.4240 
# > df_3
#   0%   10%   20%   30%   40%   50%   60%   70%   80%   90%  100% 
# 5.25  5.56  5.76  6.58  6.89  7.04  7.71  7.91  8.47  8.84 12.50

though I think it is better to keep the results in a list.