title: R: regexpr() how to use a vector in pattern parameter
body: <p>I would like to learn the positions of terms from a dictionary found in a set of short texts. The problem is in the last lines of the following code roughly based on  <a href="https://stackoverflow.com/questions/32545180/from-of-list-of-strings-identify-which-are-human-names-and-which-are-not">From of list of strings, identify which are human names and which are not</a></p>
  
  <pre><code>library(tm)
  
  pkd.names.quotes &lt;- c(
    "Mr. Rick Deckard",
    "Do Androids Dream of Electric Sheep",
    "Roy Batty",
    "How much is an electric ostrich?",
    "My schedule for today lists a six-hour self-accusatory depression.",
    "Upon him the contempt of three planets descended.",
    "J.F. Sebastian",
    "Harry Bryant",
    "goat class",
    "Holden, Dave",
    "Leon Kowalski",
    "Dr. Eldon Tyrell"
  ) 
  
  
  firstnames &lt;- c("Sebastian", "Dave", "Roy",
                  "Harry", "Dave", "Leon",
                  "Tyrell")
  
  dict  &lt;- sort(unique(tolower(firstnames)))
  
  corp &lt;- VCorpus(VectorSource(pkd.names.quotes))
  #strange but Corpus() gives wrong segment numbers for the matches.
  
  tdm  &lt;-
    TermDocumentMatrix(corp, control = list(tolower = TRUE, dictionary = dict))
  
  inspect(corp)
  inspect(tdm)
  
  View(as.matrix(tdm))
  
  data.frame(
    Name      = rownames(tdm)[tdm$i],
    Segment = colnames(tdm)[tdm$j],
    Content = pkd.names.quotes[tdm$j],
    Postion = regexpr(
      pattern = rownames(tdm)[tdm$i],
      text = tolower(pkd.names.quotes[tdm$j])
    )
  )
  </code></pre>
  
  <p>The output is with a warning and only the first line correct.</p>
  
  <pre><code>       Name Segment          Content Postion
  1       roy       3        Roy Batty       1
  2 sebastian       7   J.F. Sebastian      -1
  3     harry       8     Harry Bryant      -1
  4      dave      10     Holden, Dave      -1
  5      leon      11    Leon Kowalski      -1
  6    tyrell      12 Dr. Eldon Tyrell      -1
  
  Warning message:
  In regexpr(pattern = rownames(tdm)[tdm$i], text = tolower(pkd.names.quotes[tdm$j])) :
    argument 'pattern' has length &gt; 1 and only the first element will be used
  </code></pre>
  
  <p>I know the solution with <strong>pattern=paste(vector,collapse="|")</strong> but my vector can be very long (all popular names). </p>
  
  <p>Can there be an easy vectorized version of this command or a solution that for each row accepts a new pattern parameter? </p>