R:找到工作日的差异 [英] R: finding difference in business days

查看:137
本文介绍了R:找到工作日的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的数据。

  e<  -  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),类= c(tbl_df,data.frame),row.names = c(NA,-6L))

找到这个任务的bizdays包。这对于这个工作正常。

 > bizdays(e $ date.2,e $ date.1)
[1] 0 0 563 0 0 0

但是我的数据包含date.2在date.1之前的情况。

  e2<  - 结构(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))

现在它出现以下错误:

 > cal<  -  Calendar(holidaysANBIMA,weekdays = c(saturday,sunday))
> bizdays(e2 $ date.2,e2 $ date.1,cal)
bizdays.Date中的错误(e2 $ date.2,e2 $ date.1,cal):
所有从日期必须大于所有日期。

我正在考虑使用ifelse()逻辑,但它给了我相同的错误。 p>

 > ifelse(e2 $ date.2  bizdays.Date中的错误(e2 $ date.2, e2 $ date.1,cal):
所有日期必须大于所有日期。

帮助赞赏。

解决方案

Nweekdays()功能改编自@J。韩元。解决方案在计算R中2个日期之间的平日数



此修改后的函数考虑到正或负的日期差异
,而上述链接已接受正日期差异的解决方案。

  library(dplyr)

e2< - structure(list(date.pr =结构(c(16524,16524,16507,16510,16510,16524,16510,5974),class =Date),
date.po = structure(c(16524,16525,16510,16517,16524, 16510,16531,15974),class =Date)),
.Names = c(date.1,date.2),class = c(tbl_df,data.frame ),row.names = c(NA,-8L))

Nweekdays< - 矢量化(
函数(a,b)
{
ifelse < b,
return(sum(!weekdays(seq(a,b,days))%in%c(Saturday,Sunday)) - 1),
return总和(!w eekdays(seq(b,a,days))%in%c(星期六,星期日)) - 1))
})


> ; e2%>%
mutate(wkd1 = format(date.1,%A),
wkd2 = format(date.2,%A),
ndays_with_wkends = ifelse ((date.2> date.1),(date.2 - date.1),(date.1 - date.2)),
ndays_no_wkends = Nweekdays(date.1,date.2))

资料来源:本地资料框[8 x 6]

date.1 date.2 wkd1 wkd2 ndays_with_wkends ndays_no_wkends
(date)(date)(chr) chr)(dbl)(dbl)
1 2015-03-30 2015-03-30星期一星期一0 0
2 2015-03-30 2015-03-31星期一星期二1 1
3 2015-03-13 2015-03-16星期五星期三3 1
4 2015-03-16 2015-03-23星期一星期一7 5
5 2015-03-16 2015-03-30星期一星期一14 10
6 2015-03-30 2015-03-16星期一星期一14 10
7 2015-03-16 2015-04-06星期一星期一21 15
8 1986-05-11 2013-09-26星期日星期四10000 7143

> e2%>%mutate(ndays_no_wkends = Nweekdays(date.1,date.2))

来源:本地数据框[8 x 3]

date.1 date .2 ndays_no_wkends
(日期)(日期)(dbl)
1 2015-03-30 2015-03-30 0
2 2015-03-30 2015-03-31 1
3 2015-03-13 2015-03-16 1
4 2015-03-16 2015-03-23 5
5 2015-03-16 2015-03-30 10
6 2015-03-30 2015-03-16 10
7 2015-03-16 2015-04-06 15
8 1986-05-11 2013-09-26 7143


Having problem calculating the date difference in business days, i.e. exclude weekends like networkdays function in Excel.

Here is my data.

e <- 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))

Found the "bizdays" package for this task. Which works fine for this one.

> bizdays(e$date.2,e$date.1)
[1]   0   0 563   0   0   0

But my data contains cases when date.2 is before date.1.

e2 <- 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))

Now it gives the following error:

> cal <- Calendar(holidaysANBIMA, weekdays=c("saturday","sunday"))
> 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.

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

> ifelse(e2$date.2 < 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.

Help appreciated.

解决方案

Nweekdays() function is adapted from @J. Won. solution at Calculate the number of weekdays between 2 dates in R

This modified function takes into account of date differences of either positive or negative, whereas the above link has accepted solution for positive date difference.

library("dplyr")

e2 <- structure(list(date.pr = structure(c(16524, 16524, 16507, 16510, 16510, 16524, 16510, 5974), class = "Date"), 
                     date.po = structure(c(16524, 16525, 16510, 16517, 16524, 16510, 16531, 15974), class = "Date")), 
                .Names = c("date.1", "date.2"), class = c("tbl_df", "data.frame"), row.names = c(NA, -8L))

Nweekdays <- Vectorize(
  function(a, b) 
  {
    ifelse(a < b, 
           return(sum(!weekdays(seq(a, b, "days")) %in% c("Saturday", "Sunday")) - 1), 
           return(sum(!weekdays(seq(b, a, "days")) %in% c("Saturday", "Sunday")) - 1))
  })


> e2 %>%
     mutate(wkd1 = format(date.1, "%A"),
            wkd2 = format(date.2, "%A"),
            ndays_with_wkends = ifelse((date.2 > date.1), (date.2 - date.1), (date.1 - date.2)), 
            ndays_no_wkends = Nweekdays(date.1, date.2))

Source: local data frame [8 x 6]

      date.1     date.2   wkd1     wkd2 ndays_with_wkends ndays_no_wkends
      (date)     (date)  (chr)    (chr)             (dbl)           (dbl)
1 2015-03-30 2015-03-30 Monday   Monday                 0               0
2 2015-03-30 2015-03-31 Monday  Tuesday                 1               1
3 2015-03-13 2015-03-16 Friday   Monday                 3               1
4 2015-03-16 2015-03-23 Monday   Monday                 7               5
5 2015-03-16 2015-03-30 Monday   Monday                14              10
6 2015-03-30 2015-03-16 Monday   Monday                14              10
7 2015-03-16 2015-04-06 Monday   Monday                21              15
8 1986-05-11 2013-09-26 Sunday Thursday             10000            7143

> e2 %>% mutate(ndays_no_wkends = Nweekdays(date.1, date.2))

Source: local data frame [8 x 3]

      date.1     date.2 ndays_no_wkends
      (date)     (date)           (dbl)
1 2015-03-30 2015-03-30               0
2 2015-03-30 2015-03-31               1
3 2015-03-13 2015-03-16               1
4 2015-03-16 2015-03-23               5
5 2015-03-16 2015-03-30              10
6 2015-03-30 2015-03-16              10
7 2015-03-16 2015-04-06              15
8 1986-05-11 2013-09-26            7143

这篇关于R:找到工作日的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆