r - 在 R 中创建具有累积计算的列
问题描述
1- 按零件号和订单号对表格进行分组 2- 对按时间开始按升序创建的组进行排序 3- 参见附图,我需要创建一个 Cum Total Span。操作以天为单位(不是小时)
这是代码示例。谢谢
structure(list(Order = c("100016572", "100016572", "100016572",
"100049101", "100049101", "100049101", "100049101", "100049101",
"100049101", "100049101"), Op = c(9997, 9997, 9997, 8391, 8392,
8393, 8394, 8395, 8396, 8397), `Op Desc` = c("PP&C Movement / Go To Stock",
"PP&C Movement / Go To Stock", "PP&C Movement / Go To Stock",
"ASSEMBLY REWORK IAW 200365779", "MACHINE SHOP IAW 200365779",
"ASSEMBLY REWORK IAW 200365779", "PP&C REWORK IAW 200365779",
"INSPECT IAW 200365779", "ASSEMBLY REWORK IAW 200365779", "PP&C REWORK IAW 200365779"
), `Part No` = c("2352841G1-516", "2352841G1-516", "2352841G1-516",
"2352841G1", "2352841G1", "2352841G1", "2352841G1", "2352841G1",
"2352841G1", "2352841G1"), WBS = c("G-CUST-01", "G-CUST-01",
"G-CUST-01", "G-6M76-01", "G-6M76-01", "G-6M76-01", "G-6M76-01",
"G-6M76-01", "G-6M76-01", "G-6M76-01"), `Work Cntr` = c("26801702",
"26801702", "26801702", "CHRP0000", "CHRP0000", "CHRP0000", "CHRP0000",
"CHRI0000", "CHRP0000", "CHRP0000"), `On Time Start` = structure(c(1557852668.803,
1557852668.803, 1557852668.803, 1579215738.477, 1579279874.08,
1579598294.2, 1579625528.097, 1579690071.267, 1583883648.167,
1584029686.74), tzone = "UTC", class = c("POSIXct", "POSIXt")),
`On Time Comp` = structure(c(1585156034.313, 1585156034.313,
1585156034.313, 1579216621.723, 1579285486.47, 1579605490.21,
1579626040.097, 1583876924.857, 1584012522.213, 1584029801.71
), tzone = "UTC", class = c("POSIXct", "POSIXt")), Span_CDay = structure(c(316.011174884259,
316.011174884259, 316.011174884259, 0.0102227546302257, 0.0649582175938068,
0.0832871527776674, 0.00592592592592593, 48.4589535879647,
1.49159775462967, 0.00133067129662743), class = "difftime", units = "days")), row.names = c(NA,
-10L), groups = structure(list(Order = c("100016572", "100049101"
), .rows = structure(list(1:3, 4:10), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
解决方案
numeric
我们可以在执行之前将“Span_CDay”列转换为cumsum
library(dplyr)
df1 %>%
arrange(Order, `Part No`, `On Time Start`) %>%
group_by(Order, `Part No`) %>%
mutate(CSum_Span_CDay = cumsum(as.numeric(Span_CDay)))
更新
如果我们想在ing 列first
之后获得 'On Time Start' 元素和 'On Time Comp' 元素之间的差异arrange
df1 %>%
arrange(Order, `Part No`, `On Time Start`) %>%
group_by(Order, `Part No`) %>%
mutate(Diff_span = as.numeric(difftime(`On Time Comp`,
first(`On Time Start`), units = 'days')),
Cum_Diff_span = cumsum(Diff_span))
推荐阅读
- git - 为什么 git 源代码树中的两行相互交叉?
- ruby - Ruby 中的 DB2 安装
- cypress - 我如何通过测试我的应用程序来修复冻结柏树
- ios - 如何避免雷达图中 X 轴值和 Y 轴值重叠?
- c# - 如何将属性绑定到 WPF 中 UserControl 中的矩形样式
- hystrix - 如何在 Hystrix 中设置 coreSize 参数?
- google-chrome-extension - 从内容脚本发送消息 - 谷歌扩展
- javascript - 将第三方 javascript 小部件动态添加到 Web 应用程序
- javascript - 使用 post 数据获取 expressjs 导致找不到 404 服务器
- javascript - 滚动顶部动画在 IOS 上不起作用