修复上述代码需要三件事:
-
添加
choice
作为函数中的参数。
-
x <-
-
使用
tidyeval
使“选择”论据起作用。
my_fun <- function(x, choice)
{x %>%
summarise(`sum of mpg` = sum(mpg),
`sum of cyl` = sum(cyl),
`sum of choice` = sum(!!choice))}
list_results <- map(dflist, my_fun, choice = quo(disp))
如果您想保持在dataframe/tible中,那么使用
nest
创造
list-columns
可能会有帮助。
mtcars2$group <- sample(c("a", "b", "c"), 32, replace = TRUE)
mtcars2 %>%
as_tibble() %>%
nest(-group) %>%
mutate(out = map(data, my_fun, quo(disp))) %>%
unnest(out)