Ben
Ben

Reputation: 21625

How do I build a data frame of dates using seq.Date multiple times

endDt<-as.Date("2012-10-1")
dts<-as.Date(c("2010-1-1", "2010-3-1", "2012-10-1"))

How do I do something like the following:

endDts<-c() #an empty date vector

for each dt in dts
    append seq.Date(dt,endDt,by="3 months") to endDts
next dt

I ran into issues trying to loop through my vector of dates, dts

Upvotes: 0

Views: 95

Answers (1)

agstudy
agstudy

Reputation: 121568

In addition to the excellent comment solution you can use mapply:

mapply(seq.Date,dts, endDt,by="3 months")

[[1]]
 [1] "2010-01-01" "2010-04-01" "2010-07-01" "2010-10-01" "2011-01-01" "2011-04-01" "2011-07-01"
 [8] "2011-10-01" "2012-01-01" "2012-04-01" "2012-07-01" "2012-10-01"

[[2]]
 [1] "2010-03-01" "2010-06-01" "2010-09-01" "2010-12-01" "2011-03-01" "2011-06-01" "2011-09-01"
 [8] "2011-12-01" "2012-03-01" "2012-06-01" "2012-09-01"

[[3]]
[1] "2012-10-01"

Upvotes: 1

Related Questions