我无法用一个非常大的数据集的工作。我有一个项目ID,购买日期和购买数量。难度分组和汇总大量的数据可能
str(Output0)
'data.frame': 183847 obs. of 3 variables:
$ D: Factor w/ 460 levels "2015-09-21","2015-09-24",..: 3 3 3 3 3 3 3 3 3 3 ...
$ P: int 1 2 3 4 5 6 7 8 9 10 ...
$ Q: num 7 1 2 1 1 1 1 1 1 1 ...
作为一个说明,P =项目ID,d =日期,以及Q =采购数量
我想用3天的时间来总结每个单项的采购数量(所以有可能仍然是重复项目ID)。例如:
P Date Purchase Q
1234 1/1/16 1
1235 1/1/16 1
1235 1/2/16 1
1235 1/3/16 1
1444 1/1/16 1
1444 1/2/16 1
1444 1/3/16 1
会是什么样子:
Output2 <- aggregate(Output0$Q, by=list(PS=P,
Date = cut(as.Date(Output0$D, format="%d/%m/%Y"),breaks="3 day")), FUN=sum)
但想出这个错误:
Error in seq.int(0, to0 - from, by) : 'to' cannot be NA, NaN or infinite
In addition: Warning messages: 1: In min.default(c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, : no non-missing arguments to min; returning Inf 2: In max.default(c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, : no non-missing arguments to max; returning -Inf
ItemID DateEndPoint Purchase Q
1234 1/1/16 1
1235 1/3/16 3
1444 1/3/16 3
我已经尝试使用我也想在其他时间段做同样的事情需要(1天,1周),所以可重现的东西将是美好的。
在响应为P·拉普安特:我想下面的它看上去很不错,除了最后一列共计所有日期的所有项目,而不是每个时期
Output1 <- POData%>%mutate(Date=as.Date(POData$`PO Date`,"%m-%d-%Y"),Date_Group=cut(Date,breaks="3 days"))%>% group_by(POData$`ItemID`,Date_Group)%>%summarise(DateEndPoint=max(Date),Purchase_Q=sum(POData$`POQty`,na.rm=TRUE))
它作为显示:
> View(Output1)
> str(Output1)
Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 116749 obs. of 4 variables:
$ POData$`Item ID`: int 11 11 11 11 11 11 11 11 11 11 ...
$ Date_Group : Factor w/ 216 levels "2015-09-21","2015-09-24",..: 4 6 11 13 14 15 18 19 24 25 ...
$ DateEndPoint : Date, format: "2015-10-02" "2015-10-08" ...
$ Purchase_Q : num 2691020 2691020 2691020 2691020 2691020 ...
- attr(*, "vars")= chr "POData$`Item ID`"
- attr(*, "drop")= logi TRUE
预先感谢您!
难道ü尝试DPLYR包? – AntonCH
@AntonCH不,我还没有 - 你会推荐什么? –
@SuttonMurray我想你在现实生活中有超过3天的例子。你想要滚动的总和(每天计算)还是只有3天的周期没有重叠? –