I am trying to not use a for loop and use a function and lapply to generate a sequence of dates separated by days and append it to data. First, I tried
x = data.frame(
date = seq.Date(from = as.Date((q)[['d1']]),to = as.Date(q[['d2']]),by = "days"),
m = (q)[['m']],
stringsAsFactors = F
)
return(x)
}
y = list(
d1 = as.Date(c("2012/01/31","2012/01/14")),
d2 = as.Date(c("2012/01/31","2012/01/21")),
m = c(100,75))
z <- do.call(rbind,lapply(y,fn))
but I get Error in [[.default(q, "d1") : subscript out of bounds and I tried to fix that using get()
Then I tried
x = data.frame(
date = seq.Date(from = as.Date(get(q)[['d1']]),to = as.Date(get(q[['d2']]),by = "days"),
m = get(q)[['m']],
stringsAsFactors = F
)
return(x)
}
y = list(
d1 = as.Date(c("2012/01/31","2012/01/14")),
d2 = as.Date(c("2012/01/31","2012/01/21")),
m = c(100,75))
z <- do.call(rbind,lapply(y,fn))
I tried this but I get Error in get(q) : invalid first argument