我在stringr包中使用str_detect,但在搜索具有多个模式的字符串时遇到问题。
这是我正在使用的代码,但是即使我的向量(“Notes-Title”)包含这些模式,它也不会返回任何内容。
filter(str_detect(`Notes-Title`, c("quantity","single")))
我想要编码的逻辑是:
搜索每一行并过滤它是否包含字符串“quantity”或“single”。
您需要使用 | 搜索中的分隔符,全部在一组“”内。
> words <- c("quantity", "single", "double", "triple", "awful")
> set.seed(1234)
> df = tibble(col = sample(words,10, replace = TRUE))
> df
# A tibble: 10 x 1
col
<chr>
1 triple
2 single
3 awful
4 triple
5 quantity
6 awful
7 triple
8 single
9 single
10 triple
> df %>% filter(str_detect(col, "quantity|single"))
# A tibble: 4 x 1
col
<chr>
1 single
2 quantity
3 single
4 single
Run Code Online (Sandbox Code Playgroud)