title: R Return position of word in a string
body: <p>I have data like this:</p>
  
  <pre><code>data &lt;- data.frame(
    text = c(
      "PARACETAMOL/CODEINE",
      "PSEUDOEPH/PARACET/CODEINE",
      "PARACETAMOL/CODEINE/DOXYLAMINE",
      "CODEINE &amp; ASPIRIN",
      "CODEINE/PARACETAMOL",
      "TEST"
    ),
    stringsAsFactors = F
  )
  </code></pre>
  
  <p>I want to return in each case, in what position CODEINE occurs, i.e</p>
  
  <pre><code>text                             position
  PARACETAMOL/CODEINE                     2
  PSEUDOEPH/PARACET/CODEINE               3
  PARACETAMOL/CODEINE/DOXYLAMINE          2
  CODEINE &amp; ASPIRIN                       1
  CODEINE/PARACETAMOL                     1
  TEST                                    0
  </code></pre>
  
  <p>I prefer a DPLYR solution to run over hundreds of rows.</p>
  
  <p>I looked at various other Stackoverflow answers, but I just can't seem to get it working. They mostly deal with word indexes and not position relative to other words. An idea would be to tokenise and then count position with something like tidytext, but I think there could be an easier way. I suspect it is some nifty REGEX. </p>
  
  <p><strong>UPDATED</strong></p>
  
  <p>I neglected to add a non CODEINE based element, both answers errors out.</p>
  
  <p>Any help would be greatly appreciated.</p>