2017-09-04 89 views
1

与向量作为输入选择带引号的列我有以下的数据帧(tibble)如何使用dplyr

library(dplyr) 

dat <- structure(list(`YY_164.XXX-ad` = c(0.004, 0, 0, 0.001, 0.001, 
0.001), `YY_165.XXX-ad` = c(0.022, 0.001, 0, 0.001, 0.002, 0 
), `YY_162.XXX-ad` = c(0.013, 0, 0, 0.001, 0.001, 0.001), `YY_163.XXX-ad` = c(0.023, 
0, 0, 0.001, 0, 0.001), `YY_166.XXX-ad` = c(0.062, 0.001, 0.001, 
0.001, 0.005, 0.001)), .Names = c("YY_164.XXX-ad", "YY_165.XXX-ad", 
"YY_162.XXX-ad", "YY_163.XXX-ad", "YY_166.XXX-ad"), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame")) 
dat 
#> # A tibble: 6 x 5 
#> `YY_164.XXX-ad` `YY_165.XXX-ad` `YY_162.XXX-ad` `YY_163.XXX-ad` 
#>    <dbl>   <dbl>   <dbl>   <dbl> 
#> 1   0.004   0.022   0.013   0.023 
#> 2   0.000   0.001   0.000   0.000 
#> 3   0.000   0.000   0.000   0.000 
#> 4   0.001   0.001   0.001   0.001 
#> 5   0.001   0.002   0.001   0.000 
#> 6   0.001   0.000   0.001   0.001 
#> # ... with 1 more variables: `YY_166.XXX-ad` <dbl> 

我想要做的是基于矢量选择栏:

pp <- c('YY_164.XXX-ad','YY_165.XXX-ad') 

我试过,但出现错误:

dat %>% 
    select_(pp) 
#> Error in overscope_eval_next(overscope, expr): object 'YY_164.XXX' not found 

怎样做正确的方式?

回答

4

什么用select是怎么回事?你试过了吗?你有不同版本的dplyr(0.7.2)吗?:

> dat %>% select(pp) 
# A tibble: 6 x 2 
    `YY_164.XXX-ad` `YY_165.XXX-ad` 
      <dbl>   <dbl> 
1   0.004   0.022 
2   0.000   0.001 
3   0.000   0.000 
4   0.001   0.001 
5   0.001   0.002 
6   0.001   0.000 
2

您需要使用.dots =参数附上变种名称:

pp <- c("YY_164.XXX-ad","YY_165.XXX-ad") 
dat %>% 
    select(.dots = pp) 
# A tibble: 6 x 2 
.dots1 .dots2 
<dbl> <dbl> 
    1 0.004 0.022 
2 0.000 0.001 
3 0.000 0.000 
4 0.001 0.001 
5 0.001 0.002 
6 0.001 0.000