【发布时间】:2017-11-04 14:54:37
【问题描述】:
我有同样的问题:
How to apply grepl for data frame
但是我得到了不想要的匹配,例如:
Complete word matching using grepl in R
当 grepl 循环遍历向量时,如何在 sapply 环境中应用 \
【问题讨论】:
标签: r match sapply grepl exact-match
我有同样的问题:
How to apply grepl for data frame
但是我得到了不想要的匹配,例如:
Complete word matching using grepl in R
当 grepl 循环遍历向量时,如何在 sapply 环境中应用 \
【问题讨论】:
标签: r match sapply grepl exact-match
您使用了一个匿名函数来应用于数据框中列的每个元素。
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
【讨论】:
我自己找到了解决方案。 在要与句子匹配的向量中的每个元素之前和之后粘贴一个空格就足够了。
vector <- paste(" ", vector, " ")
matches <- sapply(vector, grepl, sentences, ignore.case=TRUE )
【讨论】: