我想编写一个函数,它以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与-
连接起来。但在我的工作中,很难以 quos(-mpg, -cyl, -disp)
的形式创建该quosure - 那么我该如何修改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()
的问题。