I am trying to go from a list of dates
dates_list = list(c(date1,date2,dateN))
to a list of lists
transformed_dates_list = list(list(date1, list(c(date1, date2)), list(c(date1, date2, dateN)))
Here is some reproducible code at my attempt
dates_list = seq_monthly(as.Date("2020-01-31"), 12)
transform_list = function(dates_list){
x = list()
for(i in seq_along(dates_list)){
x[i] = dates_list[1:i]
}
return(x)
}
temp = transform_list(dates_list)
This turns the dates into numbers and does not keep the nested list structure. I tried changing the vector elements into characters but that also did not fix the nested list structure issue.
Thank you in advance for your help!
Edit: I'm sorry I forgot to include the seq monthly function I was using. However, both answers below do this more efficiently.
require(lubridate)
seq_monthly = function(from,length.out) {
return(from %m+% months(c(0:(length.out-1))))
}