我正在尝试在分组的 df 上使用 gapply 来获取项目时间输入的时间表。
下面我想得到一个列,该列将根据他们预订时间的最早日期和他们预订时间的最晚日期之间的工作时间为一个人提供可用的工作时间。
library("dplyr")
library("stringr")
library("bizdays")
library("nlme")
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"))
#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
我测试了我的功能以确保它按预期工作。
> 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 文档说我应该得到一个数据框输出。我想将那个输出与我的原始数据结合起来,这样我就可以计算人们的利用率。
任何想法将不胜感激。