R: finding difference in business days
<p>Having problem calculating the date difference in business days, i.e. exclude weekends like networkdays function in Excel.</p>

<p>Here is my data.</p>

<pre><code>e &lt;- structure(list(date.pr = structure(c(15909, 15933, 16517, 15961, 15974, 15978), class = "Date"), date.po = structure(c(15909, 15933, 15954, 15961, 15974, 15978), class = "Date")), .Names = c("date.1", "date.2"), class = c("tbl_df", "data.frame"), row.names = c(NA, -6L))
</code></pre>

<p>Found the "bizdays" package for this task. Which works fine for this one.</p>

<pre><code>&gt; bizdays(e$date.2,e$date.1)
[1]   0   0 563   0   0   0
</code></pre>

<p>But my data contains cases when date.2 is before date.1.</p>

<pre><code>e2 &lt;- structure(list(date.pr = structure(c(15909, 15933, 16517, 15961, 5974, 15978, 15978), class = "Date"), date.po = structure(c(15909, 15933, 15954, 15961, 15974, 15978, 15979), class = "Date")), .Names = c("date.1", "date.2"), class = c("tbl_df", "data.frame"), row.names = c(NA, -7L))
</code></pre>

<p>Now it gives the following error:</p>

<pre><code>&gt; cal &lt;- Calendar(holidaysANBIMA, weekdays=c("saturday","sunday"))
&gt; bizdays(e2$date.2,e2$date.1,cal)
Error in bizdays.Date(e2$date.2, e2$date.1, cal) : 
  All from dates must be greater than all to dates.
</code></pre>

<p>I'm thinking using the ifelse() logic, but it gives me the same error.</p>

<pre><code>&gt; ifelse(e2$date.2 &lt; e2$date.1, NA, bizdays(e2$date.2,e2$date.1,cal))
Error in bizdays.Date(e2$date.2, e2$date.1, cal) : 
  All from dates must be greater than all to dates.
</code></pre>

<p>Help appreciated.</p>