r - 查找使用 R 取消的连续天数
问题描述
我正在考虑为我的员工的出勤情况制作记分卡,我需要帮助来计算分数。我的条件是,如果员工取消一天是-1分,但是如果他们连续多天取消,它仍然只是-1分。
例如:
employee workdate reason
employee1 7/7/19 CAOF
employee1 7/19/19 CAOF
employee1 8/30/19 PUL
employee1 10/02/19 CAOF
employee1 10/9/19 CAOF
employee1 10/10/19 CAOF
(数据实际样子的图片)
因此,有了这些数据,我会看到员工 1 在此时间段内取消了 5 天(原因 CAOF)。然而上一次他连续两天取消比赛,所以对他只算一分。所以他在这段时间内的得分是-4分。
我只有简单的 R 知识,但我正在努力学习,谁能帮我开始?
解决方案
如果您应用diff
跨日期(假设为sorted),则可以过滤掉低于阈值的日期。例如,
dat$workdate <- as.Date(dat$workdate, "%m/%d/%y")
dat$datediff <- ave(as.integer(dat$workdate), dat$employee, FUN = function(z) c(Inf, diff(z)))
dat
# employee workdate reason datediff
# 1 employee1 2019-07-07 CAOF Inf
# 2 employee1 2019-07-19 CAOF 12
# 3 employee1 2019-08-30 PUL 42
# 4 employee1 2019-10-02 CAOF 33
# 5 employee1 2019-10-09 CAOF 7
# 6 employee1 2019-10-10 CAOF 1
或者您可以ave
只使用生成一个逻辑变量:
dat$usereason <- ave(as.integer(dat$workdate), dat$employee, FUN = function(z) c(TRUE, diff(z) > 1))
dat
# employee workdate reason datediff usereason
# 1 employee1 2019-07-07 CAOF Inf 1
# 2 employee1 2019-07-19 CAOF 12 1
# 3 employee1 2019-08-30 PUL 42 1
# 4 employee1 2019-10-02 CAOF 33 1
# 5 employee1 2019-10-09 CAOF 7 1
# 6 employee1 2019-10-10 CAOF 1 0
请注意,这会ave
强制输出与其x=
参数的类相同,因此如果不从外部重新分类,我们就无法在此处(我知道)返回文字TRUE
/ 。FALSE
数据:
dat <- structure(list(employee = c("employee1", "employee1", "employee1", "employee1", "employee1", "employee1"), workdate = c("7/7/19", "7/19/19", "8/30/19", "10/02/19", "10/9/19", "10/10/19"), reason = c("CAOF", "CAOF", "PUL", "CAOF", "CAOF", "CAOF")), class = "data.frame", row.names = c(NA, -6L))
推荐阅读
- javascript - React 路由器在多项目 React 应用程序中处理未定义的路由
- javascript - npm WARN 使用 --force 禁用推荐的保护
- c - 分配结构值时 const 关键字有什么用?
- r - 对具有相似字符串的行求和
- reactjs - 如何将用户输入设置为大写
- wordpress - 海洋 wp 中头部站点上方的内容
- vim-plugin - neovim :PlugUpdate 对所有插件产生权限被拒绝
- c - 在内核中禁用 TCP 堆栈并创建用户空间 TCP 堆栈——称其为服务器——可能是什么挑战
- python - 具有 tcia 数据的医学成像
- reactjs - 将 JSON 变量从 REACT JS 传递到 TypeScript