title: Function works fine alone, but returns error with apply functions: attempt to select less than one element in get1index
body: <p>I have a list of character vectors that looks like this: </p>
  
  <pre><code>    [[1]]
      [1]   "medical"             "center"              "name
      [7] "laboratory"          "medicine"            "william"                 
      [13] "laboratories"        "2431"                "highway"             
      ...
      [680]
  
      ...
  
      [[100]]
      ..
      [590]
  </code></pre>
  
  <p>Each member of the list represents a patient and the character vectors per member are their tokenized medical reports. I'm mining each member of the list for certain parameters and am using the following code to do so:</p>
  
  <pre><code>    f &lt;- function(x, phrase, n_words = 3L, upto = NULL) {
      x &lt;- paste0(x, collapse = ' ')
      word &lt;- '\\b\\w+\\b\\s*'
      p &lt;- if (!is.null(upto))
              sprintf('(?:%s)\\s*((%s)+)%s|.', phrase, word, upto)
      else sprintf('(?:%s)\\s*((%s){1,%s})|.', phrase, word, n_words)
      trimws(gsub(p, '\\1', x))
     }
  </code></pre>
  
  <p>This function has worked great when using it on single character vector objects. For example:</p>
  
  <pre><code>    &gt;f(P1, "histology results", upto = "diagnosed by"))
      [1] highly differentiated, stage 4 out of 4
  </code></pre>
  
  <p>Where P1 is a character object of tokenized words. </p>
  
  <p>However, with lists and using the lapply function, I get an error. </p>
  
  <pre><code>   &gt; lapply(list, f, list[[i]], "histology results", upto = "diagnosed by")
      Error in list[[i]] : 
           attempt to select less than one element in get1index
  </code></pre>
  
  <p>When I run the function selecting the individual members of the list, it works fine for every member, with no errors thrown. Just as one example:</p>
  
  <pre><code>   &gt; f(list[[2]], "histology results", upto = "diagnosed by")
     [1] "mildly differentiated stage 1 of 4"
  </code></pre>
  
  <p>What am I doing wrong?        </p>