我想在mutate()
中向pmap()
传递一堆列。之后,我想选择这些相同的列。
目前,我将一组列名作为quosure传递给pmap()
,这样做很好,虽然我不知道这是否是“正确”的方法。但我无法弄清如何在select()
中使用相同的quosure/list。
我几乎没有使用tidyeval的经验,只是通过玩耍才做到了这一点。我想象中肯定有一种方法可以同时用于pmap()
和select()
,最好不必将我的每个列名都放在引号中,但我还没有找到它。
library(dplyr)
library(rlang)
library(purrr)
df <- tibble(a = 1:3,
b = 101:103) %>%
print
#> # A tibble: 3 x 2
#> a b
#> <int> <int>
#> 1 1 101
#> 2 2 102
#> 3 3 103
cols_quo <- quo(list(a, b))
df2 <- df %>%
mutate(outcome = !!cols_quo %>%
pmap_int(function(..., word) {
args <- list(...)
# just to be clear this isn't what I actually want to do inside pmap
return(args[[1]] + args[[2]])
})) %>%
print()
#> # A tibble: 3 x 3
#> a b outcome
#> <int> <int> <int>
#> 1 1 101 102
#> 2 2 102 104
#> 3 3 103 106
# I get why this doesn't work, but I don't know how to do something like this that does
df2 %>%
select(!!cols_quo)
#> Error in .f(.x[[i]], ...): object 'a' not found