【发布时间】:2018-09-13 09:27:40
【问题描述】:
我有以下示例数据框“df”,其中包含文本的变量“Text”:
df:
Text
1 I like blue shoes.
2 Black is great!
3 Pink and grey books.
4 I don't like grey trousers.
5 Yellow is my favorite colour
6 No more green!
7 Cars are red.
8 I have a pink bike
我使用以下代码过滤每个包含至少一个列出的单词的案例,效果非常好:
library(tidyverse)
library(igraph)
library(stringi)
library(stringr)
filter <- c("blue","green","yellow","red")
df2 <-
df %>%
filter(str_detect(tolower(df$Text), paste(filter, collapse = "|")))
df2:
Text
1 I like blue shoes.
5 Yellow is my favorite colour
6 No mor green!
7 Cars are red.
作为附加条件,我现在想添加“粉红色”和“灰色”的组合,过滤上面列出的至少一个词或组合。我想要的数据框如下所示:
df2:
Text
1 I like blue shoes.
3 Pink and grey books.
5 Yellow is my favorite colour
6 No mor green!
7 Cars are red.
你知道我怎么去那里吗? 提前致谢!
【问题讨论】:
-
请在您的问题中提供最少的、可重复的、有代表性的示例。对数据使用 dput() 并指定所有带有库的非基础包 ...