有没有一种方法可以根据 2 个字符串的匹配来过滤行。例如,我想获取名称包含
won
和 le
的所有行。
df <- data.frame(name = c("Cathy Wu","won Xion le","Matt le won","stephen leuig"),
value = 5:4)
name value
<chr> <int>
Cathy le 5
won Xion le 6
Matt le won 7
stephen won 8
James Matt 9
我正在寻找的输出是;
name value
<chr> <int>
won Xion le 6
Matt le won 7
如果我尝试
df %>% filter(str_detect(name,"won|le"))
那么结果如下,因为这里它正在做 or
(|
)
name value
<chr> <int>
Cathy le 5
won Xion le 6
Matt le won 7
stephen won 8
我正在寻找类似
"won&&le"
的东西。我可以使用 str_detect
. 来实现这一点吗
这里有几种不同的方法:
filter(df, str_detect(name, "won"), str_detect(name, "le")) # using multiple str_detect calls
filter(df, str_detect(name, "(?=.*won)(?=.*le)")) # using lookaheads
filter(df, str_detect(name,"won.*le|le.*won")) # jared's first answer
filter(df, str_detect(name, "won") & str_detect(name, "le")) # another way similar to #1
要匹配单词,而不是将字符串作为较大单词的一部分进行匹配,正如 Jared 评论的那样,您可以在要查找的每个单词的两侧添加“”,例如:
filter(df, str_detect(name, "(?=.*\\bwon\\b)(?=.*\\ble\\b)"))