changing dates based on the duration

Hi all,

I have a following data frame

 duration = c(2,2,3,3,3)
> start = c("2022-01-01","2022-01-01","2022-02-02","2022-02-02","2022-02-02")
> finish = c("2022-01-02","2022-01-02","2022-02-02","2022-02-03","2022-02-04")
> df <- cbind(start,finish,duration) %>% data.frame()

Currently, each row comes from a copied one and has the same date as the predecessor in a group (in my original dataframe there are also columns allowing for grouping, like name). I would like to change the start column date value based on the duration so that the first row of a new row is always the same, and the following rows have increased value n+1 based on the duration and their distance from the parent row, so the outcome would be the following:

 start = c("2022-01-01","2022-01-02","2022-02-02","2022-02-03","2022-02-04")
> finish = c("2022-01-02","2022-01-02","2022-02-02","2022-02-03","2022-02-04")
> df <- cbind(start,finish,duration) %>% data.frame()

I have tried with for loop and if_else but nothing seems to work for me. Thank you for any hints!

I think your example is not correct. The two dfs are identical. Did you intend them to be matrices rather than a data.frame?

Thank you, @jrkrideau .
They should be data frames. They should be identical when it comes to structure, but with different information. If you look at them the day dates are different in two dfs.

I may be missing something but those dates look the same to me.

Disregard, I see the difference now.

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.