【发布时间】:2019-11-16 10:17:37
【问题描述】:
我在 stringr 包中使用 str_detect,但在搜索超过一种模式。
这是我正在使用的代码,但是即使我的向量(“Notes-Title”)包含这些模式,它也没有返回任何内容。
filter(str_detect(`Notes-Title`, c("quantity","single")))
我要编码的逻辑是:
搜索每一行,如果它包含字符串“数量”或“单个”,则对其进行过滤。
【问题讨论】:
标签: stringr
我在 stringr 包中使用 str_detect,但在搜索超过一种模式。
这是我正在使用的代码,但是即使我的向量(“Notes-Title”)包含这些模式,它也没有返回任何内容。
filter(str_detect(`Notes-Title`, c("quantity","single")))
我要编码的逻辑是:
搜索每一行,如果它包含字符串“数量”或“单个”,则对其进行过滤。
【问题讨论】:
标签: stringr
您需要使用 |搜索中的分隔符,都在一组“”中。
> 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
【讨论】:
str_detect
mutate(col = tolower(col)) 前面的列以删除大写