-1

I have the same problem as in :

How to apply grepl for data frame

But I'm getting undesired matches, as in :

Complete word matching using grepl in R

How do I apply the \< or \b solution in a sapply environment when grepl is looping through vectors?

Antonio
  • 158
  • 1
  • 14

2 Answers2

2

You'd used an anonymous function to be applied to each element of the columns in the data frame.

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
Jake Kaupp
  • 7,892
  • 2
  • 26
  • 36
  • Thank you, but what I need is a bit different, as in the first link. A vector contains sentences, the other words. I need a matrix showing me which sentences contain which words, but only if they are complete. So I should apply the function you are describing to a vector I guess, not sure how to do it though. Thanks for your help. – Antonio Nov 06 '17 at 10:18
0

I found a solution myself. It's sufficient to paste a blank space before and after each element in the vector to be matched with the sentences.

vector <- paste(" ", vector, " ")

matches <- sapply(vector, grepl, sentences, ignore.case=TRUE )
Antonio
  • 158
  • 1
  • 14