title: r - Find String in Any Elements of a List and Then Use the Name of That List Element
body: <p>I am trying see if the string exists in any of the elements in a list and then use the name of that list if it does exist.</p>
  
  <p>Right now, I am using <code>case_when</code> and <code>grepl</code>, but I'd like a more efficient way to look through my <code>named_list</code> and then use the name.</p>
  
  <pre><code>
  fruit &lt;- c("apple", "banana", "pear")
  drinks &lt;- c("beer", "wine", "coffee")
  stuff &lt;- c("chair", "couch", "desk")
  
  
  df &lt;- data.frame(foo = sample(x = c(fruit, drinks, stuff), size = 10, replace = T),
                   x = sample(1:500, size = 10, replace = T))
  
  df
  
  # foo    x
  # pear   449
  # wine   241
  # pear   53
  # couch  72
  # banana 443
  # coffee 172
  # desk   126
  # desk   183
  # desk   7
  # banana 73
  
  df %&gt;%
    mutate(category = case_when(grepl(paste0(fruit, collapse = "|"), foo, ignore.case = T) ~ "fruit",
                                grepl(paste0(drinks, collapse = "|"), foo, ignore.case = T) ~ "drinks",
                                grepl(paste0(stuff, collapse = "|"), foo, ignore.case = T) ~ "stuff"))
  
  # foo    x   category
  # pear   449 fruit
  # wine   241 drinks
  # pear   53  fruit
  # couch  172 stuff
  # banana 443 fruit
  # coffee 172 drinks
  # desk   126 stuff
  # desk   183 stuff
  # desk   7   stuff
  # banana 73  fruit
  
  # create named list of lists/vectors
  named_list &lt;- Hmisc::llist(fruit, drinks, stuff)
  named_list
  </code></pre>