简体   繁体   English

多个列中的R grep搜索模式

[英]R grep search patterns in multiple columns

I have a data frame like as follows: 我有如下数据框:

Col1    Col2    Col3
A       B       C
D       E       F
G       H       I

I am trying to keep lines matching 'B' in 'Col2' OR F in 'Col3', in order to get: 我试图保持与“ Col2”中的“ B”或“ Col3”中的F匹配的行,以便获得:

Col1    Col2    Col3
A       B       C
D       E       F

I tried: 我试过了:

data[(grep("B",data$Col2) || grep("F",data$Col3)), ]

but it returns the entire data frame. 但它返回整个数据帧。

NOTE: it works when calling the 2 grep one at a time. 注意:一次调用2个grep时,它可以工作。

Or using a single grepl after paste ing the columns 或在paste列后使用单个grepl

df1[with(df1, grepl("B|F", paste(Col2, Col3))),]
#  Col1 Col2 Col3
#1    A    B    C
#2    D    E    F
with(df1, df1[ Col2 == 'B' | Col3 == 'F',])
#   Col1 Col2 Col3
# 1    A    B    C
# 2    D    E    F

Using grepl 使用grepl

with(df1, df1[ grepl( 'B', Col2) | grepl( 'F', Col3), ])
#   Col1 Col2 Col3
# 1    A    B    C
# 2    D    E    F

Data: 数据:

df1 <- structure(list(Col1 = c("A", "D", "G"), Col2 = c("B", "E", "H"
), Col3 = c("C", "F", "I")), .Names = c("Col1", "Col2", "Col3"
), row.names = c(NA, -3L), class = "data.frame")

The data.table package makes this type of operation trivial due to its compact and readable syntax. data.table包由于其紧凑且易读的语法而使这种类型的操作变得微不足道。 Here is how you would perform the above using data.table: 这是您使用data.table执行上述操作的方式:

> df1 <- structure(list(Col1 = c("A", "D", "G"), Col2 = c("B", "E", "H"
+ ), Col3 = c("C", "F", "I")), .Names = c("Col1", "Col2", "Col3"
+ ), row.names = c(NA, -3L), class = "data.frame")

> library(data.table)
> DT <- data.table(df1)
> DT
   Col1 Col2 Col3
1:    A    B    C
2:    D    E    F
3:    G    H    I

> DT[Col2 == 'B' | Col3 == 'F']
   Col1 Col2 Col3
1:    A    B    C
2:    D    E    F
> 

data.table performs its matching operations with with=TRUE by default. data.table默认执行with = TRUE的匹配操作。 Note that the matching is much faster if you set keys on the data but that is for another topic. 请注意,如果您在数据上设置键,则匹配要快得多,但这是另一个主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM