使用&的子设定数据表和grepl

时间:2016-09-12 05:04:49

标签: r data.table subset grepl

我的数据集的列名如下:

names(d)
 [1] "Code"        "LX(RI)"      "LX(VO)"      "LX(MV)"      "LX(WC189)" "LX(WC035)"
 [7] "NX(RI)"      "NX(VO)"      "NX(MV)"      "NX(WC189)"   "NX(WC035)" "AX(RI)"     
[13] "AX(VO)"      "AX(MV)"      "AX(WC189)"   "AX(WC035)"   "SX3I(RI)"  "SXI(VO)"    
[19] "SXI(MV)"     "TX(RI)"      "TX(VO)"      "TX(MV)"      "TX(WC189)" "TX(WC035)"

每列有几千行与之关联。我想要做的是使用grepl根据以RI结尾并保留Code列的数据表的列进行子集化。

目前我已经研究了如何将所有RI列子集化为新的data.table,但我无法弄清楚如何包含Code列。

我目前:

RI <- d[, grepl("\\(RI", names(d)), with = FALSE]

这给了我想要的东西:

names(RI)
[1] "LX(RI)" "NX(RI)" "AX(RI)" "SX3I(RI)" "TX(RI)"

我一直在尝试(注意我已经包含了&amp; Code):

RI <- d[, grepl("\\(RI&Code", names(d)), with = FALSE]

我想要返回一个包含以下列的数据表:

[1] "LX(RI)" "NX(RI)" "AX(RI)" "SX3I(RI)" "TX(RI)" "Code"

以上是我想要的输出。但是代码什么都不做,并返回一个空数据表。

几个问题:

  • 我可以使用&amp;在grepl?如果是这样,我的例子是&amp;不正确的?
  • 如果没有,是否有关于如何为RI列和代码进行子集的建议?

1 个答案:

答案 0 :(得分:0)

试试这个

ab <- c("Code","LX(RI)","LX(VO)","LX(MV)","TX(RI)","NX(RI)","NX(RI)")


ab[grepl("Code|RI",ab)]

[1] "Code"   "LX(RI)" "TX(RI)" "NX(RI)" "NX(RI)"