【发布时间】:2018-10-31 22:00:33
【问题描述】:
我想弄清楚我是否可以使用提供给purrr::pmap() 的参数的list 也可以使用purrr::set_names() 从此函数中命名输出列表 的元素。
例如,这是一个简单的示例,我使用pmap 为来自不同数据帧的一些变量创建汇总,跨分组变量。
# setup
library(tidyverse)
library(groupedstats)
set.seed(123)
# creating the dataframes
data_1 <- tibble::as.tibble(iris)
data_2 <- tibble::as.tibble(mtcars)
data_3 <- tibble::as.tibble(airquality)
# creating a list
purrr::pmap(
.l = list(
data = list(data_1, data_2, data_3),
grouping.vars = alist(Species, c(am, cyl), Month),
measures = alist(c(Sepal.Length, Sepal.Width), wt, c(Ozone, Solar.R, Wind))
),
.f = groupedstats::grouped_summary
) %>% # assigning names to each element of the list
purrr::set_names(x = ., nm = alist(data_1, data_2, data_3))
# output
#> $data_1
#> # A tibble: 6 x 16
#> Species type variable missing complete n mean sd min p25
#> <fct> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 setosa nume~ Sepal.L~ 0 50 50 5.01 0.35 4.3 4.8
#> 2 setosa nume~ Sepal.W~ 0 50 50 3.43 0.38 2.3 3.2
#> 3 versic~ nume~ Sepal.L~ 0 50 50 5.94 0.52 4.9 5.6
#> 4 versic~ nume~ Sepal.W~ 0 50 50 2.77 0.31 2 2.52
#> 5 virgin~ nume~ Sepal.L~ 0 50 50 6.59 0.64 4.9 6.23
#> 6 virgin~ nume~ Sepal.W~ 0 50 50 2.97 0.32 2.2 2.8
#> # ... with 6 more variables: median <dbl>, p75 <dbl>, max <dbl>,
#> # std.error <dbl>, mean.low.conf <dbl>, mean.high.conf <dbl>
#>
#> $data_2
#> # A tibble: 6 x 17
#> am cyl type variable missing complete n mean sd min p25
#> <dbl> <dbl> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 6 nume~ wt 0 3 3 2.75 0.13 2.62 2.7
#> 2 1 4 nume~ wt 0 8 8 2.04 0.41 1.51 1.78
#> 3 0 6 nume~ wt 0 4 4 3.39 0.12 3.21 3.38
#> 4 0 8 nume~ wt 0 12 12 4.1 0.77 3.44 3.56
#> 5 0 4 nume~ wt 0 3 3 2.94 0.41 2.46 2.81
#> 6 1 8 nume~ wt 0 2 2 3.37 0.28 3.17 3.27
#> # ... with 6 more variables: median <dbl>, p75 <dbl>, max <dbl>,
#> # std.error <dbl>, mean.low.conf <dbl>, mean.high.conf <dbl>
#>
#> $data_3
#> # A tibble: 15 x 16
#> Month type variable missing complete n mean sd min p25
#> <int> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 5 inte~ Ozone 5 26 31 23.6 22.2 1 11
#> 2 5 inte~ Solar.R 4 27 31 181. 115. 8 72
#> 3 5 nume~ Wind 0 31 31 11.6 3.53 5.7 8.9
#> 4 6 inte~ Ozone 21 9 30 29.4 18.2 12 20
#> 5 6 inte~ Solar.R 0 30 30 190. 92.9 31 127
#> 6 6 nume~ Wind 0 30 30 10.3 3.77 1.7 8
#> 7 7 inte~ Ozone 5 26 31 59.1 31.6 7 36.2
#> 8 7 inte~ Solar.R 0 31 31 216. 80.6 7 175
#> 9 7 nume~ Wind 0 31 31 8.94 3.04 4.1 6.9
#> 10 8 inte~ Ozone 5 26 31 60.0 39.7 9 28.8
#> 11 8 inte~ Solar.R 3 28 31 172. 76.8 24 107
#> 12 8 nume~ Wind 0 31 31 8.79 3.23 2.3 6.6
#> 13 9 inte~ Ozone 1 29 30 31.4 24.1 7 16
#> 14 9 inte~ Solar.R 0 30 30 167. 79.1 14 117.
#> 15 9 nume~ Wind 0 30 30 10.2 3.46 2.8 7.55
#> # ... with 6 more variables: median <dbl>, p75 <dbl>, max <dbl>,
#> # std.error <dbl>, mean.low.conf <dbl>, mean.high.conf <dbl>
由reprex package (v0.2.1) 于 2018 年 10 月 31 日创建
从这里可以看出,purrr::pmap 中的 data 参数和 purrr::set_names 中的 nm 参数的内容完全相同 ((data_1, data_2, data_3))。我想避免这种重复(这对于3 元素来说似乎是不必要的,但我有一个更大的参数列表)。我无法将此列表分配给单独的对象,因为在一种情况下它是list,而另一种情况下输入为alist。
我该怎么做?
【问题讨论】: