[英]r: iterating through all elements of several columns to detect a phrase
我正在尝试遍历包含文本文件的数据框中的几列。
我想检查第 7 列到第 16 列的每个条目,以查看是否有任何文本文件包含某个短语。
每次检测到短语时,我想将它出现的次数增加 1。
这看起来很简单。 我想我应该遍历列和行,但我似乎无法确切地弄清楚如何做到这一点。
有什么建议? 提前感谢您的任何见解。
fc_count <- 0
for (col in profiles[7:16]){
for (row in 1:nrow(profiles)){
if(isTRUE(grepl("my name is jeff", row)) == TRUE){
fc_count = fc_count + 1
}
}
}
fc_count
我们可以使用lapply
循环第 7 到 16 列,应用grepl
,使用pattern
来获取逻辑向量list
, Reduce
,通过添加 ( +
) 将其转换为单个整数向量,然后通过sum
获得sum
sum(Reduce(`+`, lapply(profiles[7:16], grepl, pattern = "my name is jeff")))
由于grepl
被vector
grepl
vector
,如果我们将 'data.frame' 转换为matrix
( matrix
是具有暗淡属性的向量),它会更紧凑
sum(grepl("my name is jeff", as.matrix(profiles[7:16])))
此外,对于for
循环,我们不需要嵌套循环,因为grepl
是矢量化的
fc_count <- 0
for(prf in profiles[7:16]){
fc_count <- fc_count + sum(grepl("my name is jeff", prf))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.