如果字符串包含任何单词列表,则R每行返回true或false [英] R return true or false per row if string contains any of a list of words
本文介绍了如果字符串包含任何单词列表,则R每行返回true或false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含一列字符串的数据集:
I have a dataset containing a column of character strings:
text <- c('flight cancelled','dog cat','coach travel','car bus','cow sheep',' high bar')
transport <- 0
df <- data.frame(text,transport)
对于每行,如果字符串"text"包含多个单词中的任何一个,我想返回1;否则返回0.我的问题是我能想到的唯一方法是使用for循环.有更有效的方法吗?我的数据集非常大,因此for循环需要永远的运行
For each row I want to return 1 if the string 'text' contains any of several words or 0 otherwise. My problem is that the only way I can think to do this is using a for loop. Is there a more efficient way of doing this? My dataset is quite large, so the for loop takes forever to run
words<- 'flight|flights|plane|seats|seat|travel|time|coach'
for (i in 1:6){
df$transport[i] <- ifelse(any(grepl(words,(str_split(as.character(df$text[i]), " ")))) == TRUE,1,0)
}
返回:
text transport
1 flight cancelled 1
2 dog cat 0
3 coach travel 1
4 car bus 0
5 cow sheep 0
6 high bar 0
推荐答案
这是一种可能性:
df <- data.frame(text = c('flight cancelled','dog cat','coach travel','car bus','cow sheep',' high bar'),
transport = 0)
words <- 'flight|flights|plane|seats|seat|travel|time|coach'
df[grep(words, df$text, value = F), "transport"] <- 1
text transport
1 flight cancelled 1
2 dog cat 0
3 coach travel 1
4 car bus 0
5 cow sheep 0
6 high bar 0
这篇关于如果字符串包含任何单词列表,则R每行返回true或false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文