title: How to improve the efficiency of finding the most recent business day for all dates in an R dataframe?
body: <p>I have a dataframe with a few million rows.  One column is a date column. I need to add a new column that has the previous business day.  I have a method to do this, but it is extremely slow.  For reasons that don't need to be explained here, I have to run this same transformation 5 times in a row.</p>
  
  <p>I have tried to make a vectorized function that does the same as the above and then compiled it using cmpfun, but that didn't seem to speed things up.  I would really appreciate any thoughts as to 1) why this is slow and 2) how to do this more efficiently.</p>
  
  <pre class="lang-r prettyprint-override"><code>library(timeDate)
  library(dplyr)
  
  # generate some random data
  nrows &lt;- 1000000
  df &lt;- tibble(row = sample(0:20, nrows, rep = TRUE),
               date = sample(seq(as.Date('2014/01/01'), as.Date('2019/03/31'), by="day"), rep = TRUE, nrows)
  )
  
  # get the holidays from 2014 until now
  holidays &lt;- holidayNYSE(seq(2014, year(today())))
  
  # subtract a day from today, check if it is a business day; if not, repeat a few times
  df &lt;- df %&gt;%
    mutate(
      previous_biz_day = date - 1,
      previous_biz_day = if_else(!isBizday(as.timeDate(previous_biz_day), holidays), previous_biz_day - 1, previous_biz_day),
      previous_biz_day = if_else(!isBizday(as.timeDate(previous_biz_day), holidays), previous_biz_day - 1, previous_biz_day),
      previous_biz_day = if_else(!isBizday(as.timeDate(previous_biz_day), holidays), previous_biz_day - 1, previous_biz_day)
    )
  </code></pre>