r - create a vector of several dates for every date input -



r - create a vector of several dates for every date input -

i have next loop creates 7 dates every input date. little illustration completes in no time @ all. actual utilize case takes much much longer, , speed up.

library(lubridate) dat <- data.frame(id=c(1, 2, 3, 4, 5, 6), datestart=c("2000-01-01", "2000-01-01", "2000-01-02", "2000-01-04", "2000-02-01", "2000-02-03")) dat$datestart <- ymd(as.character(dat$datestart)) (i in 1:nrow(dat)) { x <- seq(dat$datestart[i], dat$datestart[i] + days(7), by="1 day") if (i==1) { y <- x } else { y <- c(y, x) } } y #[1] "1999-12-31 19:00:00 est" "2000-01-01 19:00:00 est" "2000-01-02 19:00:00 est" #[4] "2000-01-03 19:00:00 est" "2000-01-04 19:00:00 est" "2000-01-05 19:00:00 est" #[7] "2000-01-06 19:00:00 est" "2000-01-07 19:00:00 est" "1999-12-31 19:00:00 est" #[10] "2000-01-01 19:00:00 est" "2000-01-02 19:00:00 est" "2000-01-03 19:00:00 est" #[13] "2000-01-04 19:00:00 est" "2000-01-05 19:00:00 est" "2000-01-06 19:00:00 est" #[16] "2000-01-07 19:00:00 est" "2000-01-01 19:00:00 est" "2000-01-02 19:00:00 est" #[19] "2000-01-03 19:00:00 est" "2000-01-04 19:00:00 est" "2000-01-05 19:00:00 est" #[22] "2000-01-06 19:00:00 est" "2000-01-07 19:00:00 est" "2000-01-08 19:00:00 est" #[25] "2000-01-03 19:00:00 est" "2000-01-04 19:00:00 est" "2000-01-05 19:00:00 est" #[28] "2000-01-06 19:00:00 est" "2000-01-07 19:00:00 est" "2000-01-08 19:00:00 est" #[31] "2000-01-09 19:00:00 est" "2000-01-10 19:00:00 est" "2000-01-31 19:00:00 est" #[34] "2000-02-01 19:00:00 est" "2000-02-02 19:00:00 est" "2000-02-03 19:00:00 est" #[37] "2000-02-04 19:00:00 est" "2000-02-05 19:00:00 est" "2000-02-06 19:00:00 est" #[40] "2000-02-07 19:00:00 est" "2000-02-02 19:00:00 est" "2000-02-03 19:00:00 est" #[43] "2000-02-04 19:00:00 est" "2000-02-05 19:00:00 est" "2000-02-06 19:00:00 est" #[46] "2000-02-07 19:00:00 est" "2000-02-08 19:00:00 est" "2000-02-09 19:00:00 est"

you can try:

y<-rep(dat$datestart,each=8)+seq(0,7*24*60*60,by=24*60*60)

you create vector repeating each element of dat$datestart 8 times , add together sec vector represents number of seconds create 0,1,2,3,...,7 days. since r recycles shorter object, obtain desired result.

r

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -