我正在处理文本数据,正在寻找解决过滤问题的方案。
我已经找到了一个解决方案,该方案可以过滤包含'Word 1' 或者 'Word 2'的行。
以下是可再现的代码:
df=data.frame(UID=c(1,2,3,4,5),Text=c("the quick brown fox jumped over the lazy dog",
"long live the king",
"I love my dog a lot",
"Tomorrow will be a rainy day",
"Tomorrow will be a sunny day"))
#Filter for rows that contain "brown" OR "dog"
filtered_results_1=dplyr::filter(df, grepl('brown|dog', Text))
然而,当我筛选同时包含'Word 1'和'Word 2'的行时,它不起作用。
#Filter for rows that contain "brown" AND "dog"
filtered_results_2=dplyr::filter(df, grepl('brown & dog', Text))
无法确定正确的语法,需要帮助,谢谢。