R 如何使用带有“的变量”;收集;及;选择";
我想给一个变量分配一个文本,然后在我的管道中使用该变量。我广泛使用R 如何使用带有“的变量”;收集;及;选择";,r,tidyverse,R,Tidyverse,我想给一个变量分配一个文本,然后在我的管道中使用该变量。我广泛使用收集和选择 在下面的示例中,我希望能够在管道代码中使用x: library(tidyverse) mtcars %>% head mtcars %>% gather(type, value, mpg:am) %>% head mtcars %>% select(mpg:am) %>% head 这就是我想要使用的变量 有什么想法吗?我们可以引用它,然后用进行评估 x <- quo(
收集
和选择
在下面的示例中,我希望能够在管道代码中使用x
:
library(tidyverse)
mtcars %>% head
mtcars %>%
gather(type, value, mpg:am) %>% head
mtcars %>% select(mpg:am) %>% head
这就是我想要使用的变量
有什么想法吗?我们可以
引用它,然后用进行评估代码>
x <- quo(mpg:am)
out1 <- mtcars %>%
gather(type, value, !! x)
x您可以尝试分别指定这两个选项吗?iey想知道@akrun在使用'mpg:am'
作为字符串时是否有办法做到这一点?@Brandon的问题是,在转换为sym
之后,它仍然有backquotesAh,因此这意味着它不可能实现?@Brandon我找不到简单的方法。您在评论中显示的选项可能是最简单的选项
mtcars %>%
gather(type, value, get(x)) %>% head
mtcars %>%
gather(type, value, !!rlang::sym(x)) %>% head
mtcars %>% select(x) %>% head
mtcars %>% select(!!rlang::sym(x)) %>% head
x <- quo(mpg:am)
out1 <- mtcars %>%
gather(type, value, !! x)
out2 <- mtcars %>%
gather(type, value, mpg:am)
identical(out1, out2)
#[1] TRUE