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)
 
    