Populate Consecutive Date by Groups

Hi,

I have a dataset as you can see in below:

WEEK App ID Total
1 123 5
1 123 6
1 123 7
1 456 3
1 456 3
1 456 2
2 123 1
2 123 5
2 123 6
2 456 2
2 456 4
2 456 3

I want to add Date column by groups (grouping by Week and App ID).

WEEK App ID Total Date
1 123 5 2020-12-28
1 123 6 2020-12-29
1 123 7 2020-12-30
1 456 3 2020-12-28
1 456 3 2020-12-29
1 456 2 2020-12-30
2 123 1 2020-12-31
2 123 5 2021-01-01
2 123 6 2021-01-02
2 456 2 2020-12-31
2 456 4 2021-01-01
2 456 3 2021-01-02

In original dataset, I have 7 days for each App but it depends. So, I don't want to populate manually multiplying with 7. I want to automatize grouping by Week and App ID.

Thank you.

Hi,

start_date = as.Date.character("2020-12-27")
do.call(rbind, args = by(df, df[, c("Week", "App")], FUN = function(d) {
  d$Date = start_date + d$Week * 1:nrow(d)
  d
}))

EDIT: SORRY NOT WORKING

This should do it

df = as.data.frame(matrix(c(1,123,5,
                            1,123,6,
                            1,123,7,
                            1,456,3,
                            1,456,3,
                            1,456,2,
                            2,123,1,
                            2,123,5,
                            2,123,6,
                            2,456,2,
                            2,456,4,
                            2,456,3), ncol = 3, byrow = T))
colnames(df) = c("Week", "App", "Total")
start_date = as.Date.character("2020-12-27")

do.call(rbind, args = by(df, df[, c("App")], FUN = function(d) {
  d$Date = start_date + 1:nrow(d) 
  d
}))

But it is not taking into acount Week
Could you tell what is expected on a less repetitive dataset
Will there be exactly only a single day between each Date for the same App ?
If not what dictate this ?
e.g.:

df = as.data.frame(matrix(c(1,123,5,
                            1,123,6,
                            1,123,7,
                            1,456,3,
                            1,456,3,
                            2,123,1,
                            2,123,5,
                            2,123,6,
                            2,456,2,
                            2,456,4,
                            2,456,3,
                            3,123,4,
                            3,123,5,
                            4,123,3,
                            4,123,3,
                            4,456,3,
                            4,456,2), ncol = 3, byrow = T))

Could you please try with the code that is in below? I think it is work.

do.call(rbind, args = by(df, df[, c("Week","App")], FUN = function(d) {
  d$Date = start_date + 1:nrow(d) 
  d
}))

Edit: No, it does not because week is a dynamic column and if week increases, date also has to increase.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.