title: Search across multiple columns with a regular expression and extract the match
body: <p>I have the following data structure and I want to check the presence of a specific string ('^881') across the columns var1 to var4. And to write the result of the match in a new column (RESULT), or an empty result, if the string is not found.</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>var1</th>
  <th>var2</th>
  <th>var3</th>
  <th>var4</th>
  <th>EXPECTED RESULT</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>8810</td>
  <td>1234</td>
  <td></td>
  <td></td>
  <td>8810</td>
  </tr>
  <tr>
  <td>1234</td>
  <td>2345</td>
  <td>3456</td>
  <td></td>
  <td></td>
  </tr>
  <tr>
  <td>1234</td>
  <td>8812</td>
  <td>2345</td>
  <td>3456</td>
  <td>8812</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I have a solution that gives me the presence of the regex-string in a 0/1-format: (Maybe there is a more concise option?!)</p>
  <pre><code>library(tidyverse)
  
  df &lt;-
    tibble(
       var1 = c('8810', '1234', '1234'),
       var2 = c('1234', '2345', '8812'),
       var3 = c('', '3456', '2345'),
       var4 = c('', '', '3456')
    )
  
  # create a new column, that indicates whether the string is present in the row (1) or not (0)
  df %&gt;% 
    mutate(FOUND = if_else( 
      across( starts_with('var'), 
              ~ str_detect(.x, '^881') 
      ) %&gt;% 
      rowSums(., na.rm=T) &gt; 0, 1, 0))
  </code></pre>
  <p>But I need the result of the match, that is the whole string.
  After some puzzling, I have found the following solution, but it has a decisive drawback. It is quite slow with large datasets (10m+ cases) and about 100 variables to search. Furthermore, it seems a bit cumbersome to me.</p>
  <pre><code>df %&gt;% 
    mutate(across(starts_with('var'),
                  ~ str_match(.x, '(^881.+)')[,1]  # gives a tibble
    ) %&gt;%   
    mutate(across(everything(),    
                  stringi::stri_na2empty)   # replace the NAs with empty strings
    ) %&gt;%  
    unite('RESULT', sep = &quot;&quot;)) # make a single column
  </code></pre>
  <p>My question: Is there a more concise way to perform this data manipulation (preferably with <code>dplyr</code>)?</p>