0

I am trying to create a dummy variable based on the string value of another variable the new dummy variable is including compound words. My question is how to get around this so it only includes the word in the quotes.

Syntax example "toke" = dataframe and "word" = a tokenized column of individual words when I run the syntax it correctly codes "product" as 1 in the new variable (toke2$product) but it will also code productdesign as a 1. I want it to only code product as 1 and productdesign as 0.

 tags <- c("product", "productdesign", "electronics")
line <- c("Bears", "Orcids", "Oranges") 
toke = data.frame(toke, tags)
toke

        toke2 <- toke%>% mutate(
          product = ifelse(str_detect(word, "product"), "1", "0"))
Kreitz Gigs
  • 369
  • 1
  • 9
  • 3
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 19 '18 at 22:13
  • You got my downvote because your `token` dataset is not reproducible. If you can fix that, I will retract my downvote and give you an upvote. – www Feb 19 '18 at 22:19
  • 1
    If you want to match only the word "product", use `str_detect(word, "^product$")`. – neilfws Feb 19 '18 at 22:20
  • Okay, I think that simple dataset will do the trick although I am not sure if it will work with the mutate and ifselse functions. – Kreitz Gigs Feb 20 '18 at 02:13
  • @neilfws thank you for the suggestion I'll give it a try! – Kreitz Gigs Feb 20 '18 at 02:13
  • I think this works for a sample dataset @www – Kreitz Gigs Feb 20 '18 at 04:07
  • Thank you for your update. I have retracted my downvote and given you an upvote. – www Feb 20 '18 at 11:50

0 Answers0