首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >应用于分组数据中组的两个日期之间的工作日数

应用于分组数据中组的两个日期之间的工作日数
EN

Stack Overflow用户
提问于 2020-03-05 19:43:36
回答 1查看 81关注 0票数 0

我试图在分组df上使用gapply来获得项目时间输入的时间线。

下面我想得到一个专栏,将有一个人的可用工作时间,根据工作时间,从最早的日期,他们预订的时间,他们的时间。

代码语言:javascript
复制
library("dplyr")
library("stringr")
library("bizdays")
library("nlme")
代码语言:javascript
复制
time_df %>% dput()
structure(list(uID = c(2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), hours = c(39, 
39, 39, 39, 19.5, 39, 31.2, 39, 39, 39, 39, 39, 39, 39, 39, 31.2, 
39, 39, 39, 39, 31.2, 39, 39, 39, 31.2, 39, 39, 39, 39, 39, 39, 
39, 39, 15.6, 39, 39, 39, 23.4, 39, 39, 23.4, 3.9, 3.9, 31.2, 
7.8, 3.9, 3.9, 3.9, 3.9, 3.9), onset = structure(c(16090, 16111, 
16097, 16083, 16125, 16104, 16076, 16118, 16139, 16216, 16209, 
16181, 16167, 16160, 16174, 16188, 16146, 16153, 16265, 16251, 
16223, 16244, 16258, 16230, 16237, 16307, 16363, 16328, 16349, 
16314, 16335, 16321, 16356, 16391, 16384, 16370, 16398, 16412, 
16377, 16405, 16433, 16139, 16160, 16153, 16209, 16251, 16272, 
16230, 16342, 16314), class = "Date"), terminus = c("2014-01-24", 
"2014-02-14", "2014-01-31", "2014-01-17", "2014-02-28", "2014-02-07", 
"2014-01-10", "2014-02-21", "2014-03-14", "2014-05-30", "2014-05-23", 
"2014-04-25", "2014-04-11", "2014-04-04", "2014-04-18", "2014-05-02", 
"2014-03-21", "2014-03-28", "2014-07-18", "2014-07-04", "2014-06-06", 
"2014-06-27", "2014-07-11", "2014-06-13", "2014-06-20", "2014-08-29", 
"2014-10-24", "2014-09-19", "2014-10-10", "2014-09-05", "2014-09-26", 
"2014-09-12", "2014-10-17", "2014-11-21", "2014-11-14", "2014-10-31", 
"2014-11-28", "2014-12-12", "2014-11-07", "2014-12-05", "2014-12-31", 
"2014-03-14", "2014-04-04", "2014-03-28", "2014-05-23", "2014-07-04", 
"2014-07-25", "2014-06-13", "2014-10-03", "2014-09-05")), row.names = c(NA, 
-50L), class = c("tbl_df", "tbl", "data.frame"))
代码语言:javascript
复制
#creating demo calendar to exclude weekends
create.calendar(name = "demo", weekdays = c("saturday","sunday"))

#function should calculate working hours between first time entry and last time entry
#ideally this will be applied to each group
timeentry = function(x){
  #creates an end_date variable from further end date in the group
  end_date = max(x$terminus)

  #creates a start_date from earliest start date in the group
  start_date = min(x$onset) 

  #returns weekdays between star and end 
  #then multiplies by 8 to get work hours
  start_date %>% bizdays(end_date, cal = "demo") * 8 
}

#group by uID and summarize
time_group = time_df %>% group_by(uID)
time_util = time_group %>% gapply(.,timeentry, which = c(onset,terminus))

Error in getGroups.data.frame(object, form, level) : 
  invalid formula for groups

我测试了我的功能,以确保它按预期工作。

代码语言:javascript
复制
> time_group %>% timeentry()
[1] 2056

> time_group$terminus %>% max()
[1] "2014-12-31"
> time_group$onset %>% min()
[1] "2014-01-06"

> bizdays("2014-01-06","2014-12-31",cal = "demo") * 8
[1] 1952

我不明白他们怎么能给出不同的结果。

我知道关于gapply和我所写的函数有一些基本的东西,但我并不真正理解。gapply说我应该得到一个数据输出。我想用我的原始数据加入这个输出,这样我就可以计算出人们的利用率。

任何想法都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-07 10:55:05

代码语言:javascript
复制
library(dplyr)
library(bizdays)

#create calendar
create.calendar(name = "demo", weekdays = c("saturday","sunday"))

#calculate working hour
time_df %>% 
  group_by(uID) %>% 
  summarise(onset_earliest  = min(onset), 
            terminus_latest = max(terminus), 
            working_hour    = bizdays(onset_earliest, terminus_latest, cal="demo") * 8)

输出是

代码语言:javascript
复制
# A tibble: 2 x 4
    uID onset_earliest terminus_latest working_hour
  <int> <date>         <chr>                  <dbl>
1     2 2014-01-06     2014-12-31              2056
2     5 2014-03-10     2014-10-03              1192

输入数据预览:

代码语言:javascript
复制
> head(time_df)
# A tibble: 6 x 4
    uID hours onset      terminus  
  <int> <dbl> <date>     <chr>     
1     2  39   2014-01-20 2014-01-24
2     2  39   2014-02-10 2014-02-14
3     2  39   2014-01-27 2014-01-31
4     2  39   2014-01-13 2014-01-17
5     2  19.5 2014-02-24 2014-02-28
6     2  39   2014-02-03 2014-02-07
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60552887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档