如何在dplyr :: tibble中基于grep选择列

时间:2017-04-10 04:02:52

标签: r dplyr

我有以下几点:

library(tidyverse)
set.seed(1)
df <- data_frame(gene=LETTERS[seq( from = 1, to = 10 )], x1.control=runif(10),x2.control=runif(10),y1.control=runif(10),y2.control=runif(10))

看起来像这样:

> df
# A tibble: 10 × 5
    gene x1.control x2.control y1.control y2.control
   <chr>      <dbl>      <dbl>      <dbl>      <dbl>
1      A 0.26550866  0.2059746 0.93470523  0.4820801
2      B 0.37212390  0.1765568 0.21214252  0.5995658
3      C 0.57285336  0.6870228 0.65167377  0.4935413
4      D 0.90820779  0.3841037 0.12555510  0.1862176
5      E 0.20168193  0.7698414 0.26722067  0.8273733
6      F 0.89838968  0.4976992 0.38611409  0.6684667
7      G 0.94467527  0.7176185 0.01339033  0.7942399
8      H 0.66079779  0.9919061 0.38238796  0.1079436
9      I 0.62911404  0.3800352 0.86969085  0.7237109
10     J 0.06178627  0.7774452 0.34034900  0.4112744

给出一个字符串,例如

wanted_col_pat = 'control'

我想获取包含该字符串的列:

# A tibble: 10 × 2
   x1.control x2.control
        <dbl>      <dbl>
1  0.26550866  0.2059746
2  0.37212390  0.1765568
3  0.57285336  0.6870228
4  0.90820779  0.3841037
5  0.20168193  0.7698414
6  0.89838968  0.4976992
7  0.94467527  0.7176185
8  0.66079779  0.9919061
9  0.62911404  0.3800352
10 0.06178627  0.7774452

我怎么能用grep和tidyverse做到这一点?

1 个答案:

答案 0 :(得分:6)

您可以使用dplyr::select_ifgrepl。例如,要匹配列名称包含“control”的所有列:

library(dplyr)
df %>%
  select_if(grepl("control", names(.)))