Detecting matching string for logical variable

Hi, apologies since I am new to this and I am sure is a fairly basic question but all the answers I come across seem far too complex for what I want to do.

I am trying to create a logical variable based on whether there is a string match for any 5 words.

df$haskeywords <- str_detect(df$text, 
pattern=fixed ("keyword1","keyword2","keyword3","keyword4","keyword5"))

Have also tried

df %>% mutate(newvar = text %in% Keywords)

with "Keywords" being a value with the 5 keywords in it.

Thank you in advance.

(I understand your anger at people not spending more time learning the language, I truly do but I lack the time and I've already spent 4 hours on tutorials trying to fix just this.)

Hi @sdm, welcome to RStudio Community.

Your second attempt should give you the logical vector you desire. Did it not work? See below for a working example.

keywords <- c("apple", "banana")

text <- c("orange", "banana", "mango", "grapes", "apple")

text %in% keywords
#> [1] FALSE  TRUE FALSE FALSE  TRUE

Created on 2020-05-17 by the reprex package (v0.3.0)

1 Like
library(tidyverse)
(df <- data.frame(text=c("keyword2","xyz","99keyword2")))
(match_vec <- c("keyword1","keyword2","keyword3","keyword4","keyword5"))
# for exact matches
df$haskeywords <- df$text %in% match_vec
df

# for contains
df$haskeywords2 <- grepl( x= df$text, 
                          pattern = paste0(match_vec,collapse = "|"))
df
1 Like

Thank you for the help and the welcome, that's very kind of you!

It didn't but I ended up using a function work-around. Is it good practice to update with your own code? Have saved your suggestion for when I finish this and go back to expanding on what I know. Thanks!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.