Apply function with Switch to a vector
<p>I'm attempting to apply the following function to a vector, using the apply family (<code>sapply</code> in this case):</p>

<pre><code>get_dates &lt;- function(text, pattern, pattern_list){

  text &lt;- str_to_lower(text)

  index &lt;- switch (pattern,
    pattern_1 = 1,
    pattern_2 = 2,
    pattern_3 = 3,
    pattern_4 = 4,
    pattern_5 = 5,
    pattern_7 = 6,
    pattern_8 = 7
  )

  regex_pattern = pattern_list[index]

  dates &lt;- str_extract(text, regex_pattern)

  return(dates)
}
</code></pre>

<p>The arguments, <em>text</em>, <em>pattern</em>, and <em>pattern_list</em> descriptions are below:</p>

<ul>
<li>text &lt;- character vector taken from a data frame</li>
<li>pattern &lt;- character vector of the same length as text, taken from same data frame as text</li>
<li>pattern_list &lt;- vector of seven different regex patterns to be applied to text depending on the value of pattern</li>
</ul>

<p>The solution I've tried is below, just using the first two elements of my text and pattern vector.</p>

<pre><code>text &lt;- c("FEB-MAY14", "JUN-AUG14")
pattern &lt;- c("pattern_8", "pattern_8")

 pattern_list &lt;- c(full_pattern_1,
             full_pattern_2,
             full_pattern_3,
             full_pattern_4,
             full_pattern_5,
             full_pattern_7,
             full_pattern_8)
</code></pre>

<p>where the elements of pattern list are below:</p>

<pre><code> [1] "\\d{2}\\s?(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\\s?  \\d{2}\\s?\\-\\s?\\d{2}\\s?(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\\s?\\d{2}"                                                                                                                                                                     
 [2] "(january|february|march|april|may|june|july|august|september|october|november|december)\\s?\\d{4}\\s?\\-\\s?(january|february|march|april|may|june|july|august|september|october|november|december)\\s?\\d{2}\\,?\\s?\\d{4}"                                                                                               
 [3] "(january|february|march|april|may|june|july|august|september|october|november|december)\\s?\\-\\s?(january|february|march|april|may|june|july|august|september|october|november|december)\\s?\\d{2}\\,\\s?\\d{4}"                                                                                                          
 [4] "\\d{2}\\s?(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\\s?\\d{4}\\s?\\-\\s?\\d{2}\\s?(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\\s?\\d{4}"                                                                                                                                                                     
 [5] "(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\\s?\\d{4}\\s?\\-\\s?(january|february|march|april|may|june|july|august|september|october|november|december)\\s?\\d{1,2}\\,\\s?\\d{4}"                                                                                                                                    
 [6] "(january|february|march|april|may|june|july|august|september|october|november|december)\\s?\\d{1,2}\\,\\s?\\d{4}\\s?\\-\\s?(january|february|march|april|may|june|july|august|september|october|november|december)\\s?\\d{2}\\,\\s?\\d{4}"                                                                                 
 [7] "(january|february|march|april|may|june|july|august|september|october|november|december|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\\s?\\-?(\\d{2,4})?\\-?\\s?(january|february|march|april|may|june|july|august|september|october|november|december|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)?\\s*\\-*\\d{2,4}"

daaa &lt;- sapply(text, 
                function(x, y, z) get_dates(x, y, z), 
                y = pattern, 
                z = pattern_list)
</code></pre>

<p>However, I get the following error when I use <code>apply</code>:</p>

<pre><code>Error in switch(pattern, pattern_1 = 1, pattern_2 = 2, pattern_3 = 3,  : 
EXPR must be a length 1 vector 
</code></pre>

<p>which doesn't make sense to me; I thought the apply family would loop through taking each element at time as it applies the function. I've used a <code>for-loop</code> to manually go through the vectors and that worked as expected:</p>

<pre><code>daaa &lt;- c()
for(i in 1:2){
daaa[i] &lt;- get_dates(text[i],
                   pattern[i],
                   pattern_list)
}
</code></pre>

<p>I've left out the nature of the actual regex patterns and the text contents because that is not the problem, at least how I see it now. If that is needed I can come up with a contrived example of the inputs, but right now the problem I'm having is with applying my function and <code>switch</code> seems to be the bottleneck.</p>