我無法用一個非常大的數據集的工作。我有一個項目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天的週期沒有重疊? –