title: dplyr lag passing vector instead of single value to invoked function
body: <p>I have a data frame like this one:</p>
  
  <pre><code>&gt; df
    threadId          threadTime
  1        1 2014-07-08 18:28:29
  2        1 2014-07-08 18:46:09
  3        2 2014-07-10 10:14:42
  4        2 2014-07-10 10:30:49
  5        3 2014-07-10 11:19:27
  6        3 2014-07-10 11:45:23
  &gt; str(df)
  'data.frame':   6 obs. of  2 variables:
   $ threadId  : num  1 1 2 2 3 3
   $ threadTime: POSIXct, format: "2014-07-08 18:28:29" "2014-07-08 18:46:09" ...
  </code></pre>
  
  <p>I am using a simple function call (it does not matter what this function does):</p>
  
  <pre><code>someFunc &lt;- function(t1, t2) { cat(t1); cat('\n'); return(1)}
  </code></pre>
  
  <p>When I invoke the function to mutate a new column on the data frame as follows, I see that the function is invoked with a vector of threadTime values instead of the value of the lagged row and current row independently. What am I missing?</p>
  
  <pre><code>df %&gt;% group_by(threadId) %&gt;% mutate(blah = someFunc(lag(threadTime), threadTime))
  </code></pre>
  
  <p>I was expecting t1 to be time in the lagged row, and t2 to be the time in the current row. Of course, the first row of the group will have NA for lagged value, and I am fine and can check for that in my function.</p>
  
  <p>Output from that function invocation for the 'three groups in the df' are as follows for just the t1 parameter. The function gets invoked with the concatenated values of 'lag(threadTime)' and also the 'threadTime'.</p>
  
  <pre><code>t1 = NA 1404858509
  t1 = NA 1405001682
  t1 = NA 1405005567
  </code></pre>
  
  <h3> UPDATE </h3>
  
  <p>Turns out that what my function does/needs matters entirely. Updating the question. I want to calculate working days and hours between lagged row time values. So, I am using a function as follows (onDays are business days, net of weekends + holidays, and onHours are business hours):</p>
  
  <pre><code>workingHours &lt;- function(t1, t2) {
    elapsedTime &lt;- seq.POSIXt(from = t1, to = t2, by = 'hour')
    elapsedTime &lt;- elapsedTime[as.Date(elapsedTime) %in% onDays &amp;
                                 as.numeric(format(elapsedTime, '%H')) %in%
                                 onHours]
    return(length(elapsedTime))
  }
  </code></pre>
  
  <p>In this case, seq.POSIXt won't take vectors for 'from' and 'to' and so I can't use vectorized lag() values. Is there a better alternative than 'looping' or using some sort of an apply() call that is perhaps only a slight improvement over a loop?</p>