Reputation: 1305
So I have a datatable df with column ID DATE and STOCK
In this table, the same ID has multiple values with their date and stock:
ID DATE STOCK
a1 2017-05-04 1
a1 2017-06-04 4
a1 2017-06-05 1
a1 2018-05-04 1
a1 2018-06-04 3
a1 2018-06-05 1
a2 2016-11-26 2
a2 ... ..
Using lubridate I can get which week a date is as follows:
dfWeeks=df[,"WEEK" := floor_date(df$`Date`, "week")]
ID DATE STOCK WEEK
a1 2017-05-04 1 2017-04-30
a1 2017-06-04 4 2017-06-04
a1 2017-06-05 1 2017-06-04
a1 2018-05-04 1 2018-04-29
a1 2018-06-04 3 2018-06-03
a1 2018-06-05 1 2018-06-03
a2 2016-11-26 2 2016-11-20
a2 ... ..
So from column DATE I know my old date is 2017-05-04
and newest date 2018-06-05
, which has about 56.71429 weeks:
dates <- c( "2017-05-04","2018-06-05")
dif <- diff(as.numeric(strptime(dates, format = "%Y-%m-%d")))/(60 * 60 * 24 * 7)
And my table has only 4 unique weeks, so the idea is to sum stock for each week and insert the missing (57-4=53 weeks) ones with 0 value in stock.
Then I can do the mean of all the weeks like
meanStock<- dfWeeks[, .(mean=sum(Stock, na.rm = T)/dif <- diff(as.numeric(strptime(c(min(Date), max(Date)), format = "%Y-%m-%d")))/(60 * 60 * 24 * 7) ), by = .(ID)]
But I don't know if it will work, Hope I made it clear and any advice or approach is welcomed.
UPDATE:
This is how I get the max and min date
max = aggregate(df$`Date`,by=list(df$ID),max)
colnames(max) = c("ID", "MAX")
min = aggregate(df$`Date`,by=list(df$ID),min)
colnames(min) = c("ID", "MIN")
test <- merge(max, min, by="ID", all=T)
Upvotes: 3
Views: 116
Reputation: 14764
Something like:
library(data.table)
setDT(df)[, DATE := as.Date(DATE)][, `:=` (st = min(DATE), end = max(DATE) + 7), by = ID][
, .(ID = ID, DATE = DATE, STOCK = STOCK, Expanded = seq(st, end, by = "week")), by = 1:nrow(df)][
, `:=` (WEEK = floor_date(Expanded, "week"), WEEK2 = floor_date(DATE, "week"))][
WEEK != WEEK2, STOCK := 0][
, .(SUM_STOCK = sum(STOCK)), by = .(WEEK, ID)]
Output (rows for the weeks of 2017-04-02
until 2017-06-11
and ID
a1
):
WEEK ID SUM_STOCK
1: 2017-04-02 a1 0
2: 2017-04-09 a1 0
3: 2017-04-16 a1 0
4: 2017-04-23 a1 0
5: 2017-04-30 a1 1
6: 2017-05-07 a1 0
7: 2017-05-14 a1 0
8: 2017-05-21 a1 0
9: 2017-05-28 a1 0
10: 2017-06-04 a1 5
11: 2017-06-11 a1 0
Upvotes: 1