title: use ifelse statement in R data.table column calculation dependent on value first row
body: <p>I have to do some regex on a large data tables (+30m rows) (actually many of these). Where one columns is either just a repeated string (same for every row or missing) and other are different strings per row. Now, if that first column value is either missing or passes some other regex, I do not want to do the regex and just return FALSE, if it not missing I want to see if the columns match. This because I do need this for thousands of data.tables and because the regex takes a couple of seconds I would like to include an ifelse statement, where the regex does not even get attempted if the statement is FALSE.</p>
  <p>this is what I attempted, but none of these work (I also tried <code>fifelse</code> and <code>if_else</code></p>
  <pre><code>library(data.table)
  set.seed(10)
  data_table_test &lt;-
    data.table(col  = rep(&quot;c&quot;, 1e6),
               col2 =  paste(
                 sample(letters, 1e6,
                        replace = T),
                 sample(letters, 1e6,
                        replace = T),
                 sep = &quot;&quot;
               ))
  
  data_table_test2 &lt;-
    data.table(col  = rep(NA, 1e6),
               col2 =  paste(
                 sample(letters, 1e6,
                        replace = T),
                 sample(letters, 1e6,
                        replace = T),
                 sep = &quot;&quot;
               ))
  
  
  data_table_test[, ':='(matching_letter_1   = stringi::stri_detect_fixed(col2, col),
                         matching_letter_2   = ifelse(is.na(data_table_test[1, col ]), F, stringi::stri_detect_fixed(col2, col))),]
  
  data_table_test2[, ':='(matching_letter_1   = stringi::stri_detect_fixed(col2, col),
                         matching_letter_2   = ifelse(is.na(data_table_test2[1, col ]), F, stringi::stri_detect_fixed(col2, col))),]
  
  </code></pre>
  <p>This does work, but is slower</p>
  <pre><code>data_table_test2[, ':='(matching_letter_1   = stringi::stri_detect_fixed(col2, col)), ][, ':='(matching_letter_1 = fifelse(is.na(matching_letter_1),  F, matching_letter_1)), ]
  
  </code></pre>
  <p><strong>EDIT</strong>
  The expected output would be something should be something like this</p>
  <p><code>data_table_test[matching_letter_1 == TRUE]</code></p>
  <p>should be the same as</p>
  <p><code>data_table_test[matching_letter_2 == TRUE]</code></p>
  <p>and</p>
  <p><code>data_table_test2[matching_letter_1 == TRUE]  </code></p>
  <p>should be the same as (both empty data.tables)</p>
  <p><code>data_table_test2[matching_letter_2 == TRUE] </code></p>
  <p>A slow, but functional tidyverse solution would be this:</p>
  <pre><code>data_table_test %&gt;%
    as_tibble() %&gt;%
    rowwise() %&gt;%
    mutate(matching_letter = ifelse(is.na(data_table_test$col[1]), F, stringi::stri_detect_fixed(col2, col))) %&gt;%
    filter(matching_letter)
  
  
  # A tibble: 75,772 x 3
  # Rowwise: 
     col   col2  matching_letter
     &lt;chr&gt; &lt;chr&gt; &lt;lgl&gt;          
   1 c     cb    TRUE           
   2 c     ce    TRUE           
   3 c     yc    TRUE           
   4 c     ch    TRUE           
   5 c     ic    TRUE           
   6 c     gc    TRUE           
   7 c     cg    TRUE           
   8 c     lc    TRUE           
   9 c     ci    TRUE           
  10 c     zc    TRUE           
  # ... with 75,762 more rows
  </code></pre>
  <p>and</p>
  <pre><code>
  data_table_test2 %&gt;%
    as_tibble() %&gt;%
    rowwise() %&gt;%
    mutate(matching_letter = ifelse(is.na(data_table_test2$col[1]), F, stringi::stri_detect_fixed(col2, col))) %&gt;%
    filter(matching_letter)
  
  
  
  # A tibble: 0 x 3
  # Rowwise: 
  # ... with 3 variables: col &lt;lgl&gt;, col2 &lt;chr&gt;, matching_letter &lt;lgl&gt;
  </code></pre>
  <p><strong>EDIT 2</strong>
  This code would do the trick, but is not the solution I need, because I need to test for many combinations of columns. I need the if statement inside of the data.table operation</p>
  <pre><code>if(is.na(data_table_test[1, col ])){
    data_table_test[, matching_letter := F, ]
  }else{
    data_table_test[, matching_letter_1 := stringi::stri_detect_fixed(col2, col),]
  }
  </code></pre>