Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何使用带有“的变量”;收集;及;选择";_R_Tidyverse - Fatal编程技术网

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您可以尝试分别指定这两个选项吗?ie
y想知道@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