I tried to use the varies answers from Expanding a sequence in a data frame to my dataframe, but nothing I tried works.
Sample Data
library(dplyr)
p1 <- c(1:5)
p2 <- as.Date(c("2013-01-01","2013-01-22","2014-02-01","2014-05-12","2015-02-22"))
p3 <- as.Date(c("2013-01-11","2013-01-30","2014-02-20","2014-05-22","2015-02-28"))
p4 <- c(11,9,20,11,7)
df2 <- data_frame(p1,p2,p3,p4)
names(df2) <- c("ID", "StartDate", "EndDate", "NoDays")
df2
Desired Result
ID datelist NoDays
1 2013-01-01 1
1 2013-01-02 1
1 2013-01-03 1
etc..
1 2013-01-10 1
1 2013-01-11 1
2 2013-01-22 1
2 2013-01-23 1
etc.
2 2013-01-28 1
2 2013-01-29 1
2 2013-01-30 1
Here are three code trials - all of which I tried in numerous variants (e.g. verious members of the apply family), but all failed (i.e. giving diverse error messages):
code example 1
datelist <- seq.Date(from = df2$StartDate, to=df2$StartDate, by="days")
code example 2
datelist <- seq.Date(from = df2$StartDate, by="days", length.out = df2$NoDays)
code example 2
datelist <- apply(df2, 1, seq.Date(from = df2$StartDate, to=df2$StartDate, by="days"))