我想编写一个函数,它将 quosure 作为参数,附加-
到 quosure,并将其传递给gather
,如下所示:
library(tidyverse)
my_gather <- function(d, not.columns) {
dg <- tidyr::gather(d, key = k, value = v, .dots = -!!!not.columns)
dg
}
de <- my_gather(mtcars, not.columns = quos(mpg, cyl, disp))
> Error in `-`(~mpg, ~cyl, ~disp) : operator needs one or two arguments
这显然是因为我需要将 quosure 的每个元素-
附加到 ,而不是将整个 quosure 附加到-
。但在我的工作中,以 - 的形式创建这个 quosure 并不容易,quos(-mpg, -cyl, -disp)
那么我该如何修改quos(mpg, cyl, disp)
以添加-
?
我希望看到与 相同的结果gather(mtcars, key = k, value = v, -mpg, -cyl, -disp)
,其中前 3 行是
mpg cyl disp k v
1 21.0 6 160 hp 110
2 21.0 6 160 hp 110
3 22.8 4 108 hp 93
这里有一个类似的问题,但没有答案,似乎没有处理quos()
而不是的问题quo()
。