首页 > 解决方案 > 在 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"))

标签: r

解决方案


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

推荐阅读