2013-09-28 94 views
0
endDt<-as.Date("2012-10-1") 
dts<-as.Date(c("2010-1-1", "2010-3-1", "2012-10-1")) 

建立日期的數據幀我怎樣做類似如下:如何使用seq.Date多次

endDts<-c() #an empty date vector 

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

我跑進試圖循環問題,通過我的日期向量, dts

+1

這不是CLE對我來說,你想要data.frame看起來像,但這裏有一個想法:'Vectorize(seq.Date)(dts,endDt,by =「3 months」)' – GSee

回答

1

除了出色的註釋解決方案,您可以使用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"