r - R中的grep搜索
问题描述
我附上了表格的图像,我想根据以下搜索条件过滤“标题”列。标题列包含文本。
word=c('COVID','coronavirus disease 19','SARS-CoV-2','2019-nCoV','nCoV','coronavirus','wuhan pneumonia','Wuhan')
搜索一个我知道我可以使用的词
merged[grep("COVID",merged$Title),"Title"]
或者
sapply(words, grepl, merged$Title) returns TRUE and FALSE. How to select the rows for which sapply is true.
解决方案
我们可以使用它lapply
和Reduce
它到一个单一的逻辑vector
与|
来子集“合并”的行,即当任何“单词”出现在“标题”列中时,我们正在选择“合并”的行
merged[Reduce(`|`, lapply(words, grepl, merged$Title)),]
另一种选择是将其作为分隔符paste
的单个字符串,其作用类似于|
OR
pat <- paste0("\\b(", paste(words, collapse="|"), ")\\b")
merged[grepl(pat, merged$Title),]
推荐阅读
- flutter - 按日期时间对列表进行排序,最新的在颤振应用程序的顶部
- swift - 更复杂的 AutoLayout 方程
- firebase - 如何过滤 DocumentReferences 列表中的 Firebase 文档?
- java - 在Android Studio中添加依赖项后重复类
- javascript - 一张一张显示数据 JavaScript
- php - 渲染错误:TypeError:无法读取 laravel vuejs 中未定义的属性标题
- linux - Eroor:您需要安装 gtk-doc >= 1.12 才能构建 GStreamer RTSP 服务器(Gstreamer RTSP 服务器交叉编译)
- reactjs - 链接中的状态未设置,返回未定义
- python - 使用存储在 python 中的多个矩阵中的数据创建和求解线性方程组的不同组合
- javascript - 单击任何一个 div 时隐藏其他 div