我遇到与以下相同的问题:
How to apply grepl for data frame
但是我得到了不受欢迎的比赛,如:
Complete word matching using grepl in R
如何应用\<当grepl循环遍历向量时,或者在一个sapply环境中的解决方案?
答案 0 :(得分:1)
您已使用匿名函数应用于数据框中列的每个元素。
vec1 <- c("I don't want to match this", "This is what I want to match")
vec2 <- c('Why would I match this?', "What is a good match for this?")
df <- data.frame(vec1,vec2)
sapply(df, function(x) grepl("\\<is\\>", x))
vec1 vec2
[1,] FALSE FALSE
[2,] TRUE TRUE
答案 1 :(得分:0)
我自己找到了解决方案。 在向量中的每个元素之前和之后粘贴一个空格就足以与句子匹配。
vector <- paste(" ", vector, " ")
matches <- sapply(vector, grepl, sentences, ignore.case=TRUE )