title: Vectorized function for dplyr::mutate()
body: <p>I am trying to write a <strong>vectorized function</strong> to compare a string to a vector of regular expressions and return TRUE if the string matches any of the regular expressions. The function should work on a single string, a vector of strings, and within <code>dplyr::mutate</code>. I'm having trouble vectorizing the function. In the below reprex, the function I would like to vectorize is <code>is_favorite</code>. I came up with work-arounds using <code>base::Vectorize</code> and <code>purrr::map</code> but these quite accomplish the goal. I also tried using <code>stringi::stri_detect_regex</code> instead of <code>stringr::str_detect</code> which produced the same results.</p>
  <p>Thank you so much for your help!</p>
  <pre class="lang-r prettyprint-override"><code>library(dplyr)
  library(tibble)
  library(stringr)
  
  favorite_cars &lt;- c(&quot;^Merc&quot;, &quot;Firebird$&quot;)
  
  mtcars &lt;-
    mtcars %&gt;% 
    tibble::rownames_to_column(&quot;car&quot;)
  
  is_favorite &lt;- function(x) {
    any(stringr::str_detect(x, favorite_cars))
  }
  
  # Success: Works on single string
  is_favorite(&quot;Merc 240D&quot;)
  #&gt; [1] TRUE
  is_favorite(&quot;Ferrari Dino&quot;)
  #&gt; [1] FALSE
  
  # Failure: On vector, returns a single boolean instead of a vector of booleans
  is_favorite(mtcars$car)
  #&gt; [1] TRUE
  
  # Failure: Add column with single boolean instead of vectorized rowwise
  mtcars %&gt;% 
    mutate(fav_car = is_favorite(car)) %&gt;% 
    head()
  #&gt;                 car  mpg cyl disp  hp drat    wt  qsec vs am gear carb fav_car
  #&gt; 1         Mazda RX4 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4    TRUE
  #&gt; 2     Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4    TRUE
  #&gt; 3        Datsun 710 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1    TRUE
  #&gt; 4    Hornet 4 Drive 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1    TRUE
  #&gt; 5 Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2    TRUE
  #&gt; 6           Valiant 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1    TRUE
  
  # Success...but not ideal
  # https://community.rstudio.com/t/vectorise-text-function-for-dplyr-mutate/53387/2
  # https://deanattali.com/blog/mutate-non-vectorized/
  # https://www.jimhester.com/post/2018-04-12-vectorize/
  v_is_favorite &lt;- Vectorize(is_favorite)
  
  mtcars %&gt;% 
    mutate(fav_car = v_is_favorite(car)) %&gt;% 
    head()
  #&gt;                 car  mpg cyl disp  hp drat    wt  qsec vs am gear carb fav_car
  #&gt; 1         Mazda RX4 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4   FALSE
  #&gt; 2     Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4   FALSE
  #&gt; 3        Datsun 710 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1   FALSE
  #&gt; 4    Hornet 4 Drive 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1   FALSE
  #&gt; 5 Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2   FALSE
  #&gt; 6           Valiant 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1   FALSE
  
  # Success...better but still not ideal
  # https://www.jimhester.com/post/2018-04-12-vectorize/
  # http://yoshidk6.hatenablog.com/entry/2018/09/05/222248
  mtcars %&gt;% 
    mutate(fav_car = purrr::map_lgl(car, is_favorite)) %&gt;% 
    head()
  #&gt;                 car  mpg cyl disp  hp drat    wt  qsec vs am gear carb fav_car
  #&gt; 1         Mazda RX4 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4   FALSE
  #&gt; 2     Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4   FALSE
  #&gt; 3        Datsun 710 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1   FALSE
  #&gt; 4    Hornet 4 Drive 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1   FALSE
  #&gt; 5 Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2   FALSE
  #&gt; 6           Valiant 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1   FALSE
  </code></pre>
  <p><sup>Created on 2020-08-28 by the <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex package</a> (v0.3.0)</sup></p>
  <p>I also consulted the following resources:</p>
  <ul>
  <li><a href="https://adv-r.hadley.nz/perf-improve.html#vectorise" rel="nofollow noreferrer">https://adv-r.hadley.nz/perf-improve.html#vectorise</a></li>
  <li><a href="https://deanattali.com/blog/mutate-non-vectorized/" rel="nofollow noreferrer">https://deanattali.com/blog/mutate-non-vectorized/</a></li>
  <li><a href="https://www.jimhester.com/post/2018-04-12-vectorize/" rel="nofollow noreferrer">https://www.jimhester.com/post/2018-04-12-vectorize/</a></li>
  <li><a href="https://community.rstudio.com/t/vectorise-text-function-for-dplyr-mutate/53387/2" rel="nofollow noreferrer">https://community.rstudio.com/t/vectorise-text-function-for-dplyr-mutate/53387/2</a></li>
  <li><a href="http://yoshidk6.hatenablog.com/entry/2018/09/05/222248" rel="nofollow noreferrer">http://yoshidk6.hatenablog.com/entry/2018/09/05/222248</a></li>
  <li><a href="https://dplyr.tidyverse.org/articles/programming.html" rel="nofollow noreferrer">https://dplyr.tidyverse.org/articles/programming.html</a></li>
  </ul>