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

如何执行以下操作:

endDts<-c() #an empty date vector

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

我在尝试遍历我的日期向量时遇到了问题,dts

4

1 回答 1

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"
于 2013-09-29T00:26:22.737 回答