title: Lookup word in data frame, return column name?
body: <p>I wish to search a dataframe (really, a categorized word list), and if the word is found, it returns the column name; if it is not found, it simply reproduces the word. The basic idea is below but I can't get it to work as expected:</p>
  
  <pre><code>
  #data frame to be searched
  words &lt;- data.frame(people=c("Mike", "Tom", "Molly", "Susan"),
             dogs=c("Rex", "Fido", "King", "Roy"))
  
  #data frame to work with
  d &lt;- data.frame(name=c("Roy","Tom", "Pat"))
  
  d %&gt;% mutate(
    returned = ifelse(name %in% d, colnames(), name)
  )
  </code></pre>
  
  <p>This returns:</p>
  
  <pre><code>  name returned
  1  Roy        2
  2  Tom        3
  3  Pat        1
  </code></pre>
  
  <p>However, it should return</p>
  
  <pre><code>  name returned
  1  Roy        dog
  2  Tom        people
  3  Pat        Pat
  </code></pre>
  
  <p>I feel like my script is close, but not sure what to do to fix it.
  Any help is appreciated!</p>