This is very similar to this question, but with an added layer. I am looking to see if a string in one column exists in another column. But since for some rows the column is empty, when I run the code below I get a lot of 'TRUE' because they just match spaces. How can I ignore spaces and just match on characters?
word <- c('Hello','','nyc', '')
keywords <- c('hello goodbye nyc','hello goodbye nyc', 'hello goodbye nyc', 'hello goodbye nyc')
df <- data.frame(word, keywords, stringsAsFactors=F)
df$word_exists <- mapply(grepl, pattern=df$keywords, x=df$word)
nzchar to check that your pattern has characters:
transform(df, word_exists=mapply(grepl, pattern=word, x=keywords) & nzchar(word)) # word keywords word_exists # 1 Hello hello goodbye nyc FALSE # 2 hello goodbye nyc FALSE # 3 nyc hello goodbye nyc TRUE # 4 hello goodbye nyc FALSE