创建带有日期的向量不是一件容易的事,而是借助seq和as.Date在R中变得容易。借助这些功能,我们可以在R中创建一个包含两个日期之间的日期的向量。但这不能以相反的顺序进行,例如,如果我们希望将将来的日期作为向量的第一个元素,则将不可能。
> V1<-seq(as.Date("2020-01-01"), as.Date("2020-02-28"), by="days") > V1 [1] "2020-01-01" "2020-01-02" "2020-01-03" "2020-01-04" "2020-01-05" [6] "2020-01-06" "2020-01-07" "2020-01-08" "2020-01-09" "2020-01-10" [11] "2020-01-11" "2020-01-12" "2020-01-13" "2020-01-14" "2020-01-15" [16] "2020-01-16" "2020-01-17" "2020-01-18" "2020-01-19" "2020-01-20" [21] "2020-01-21" "2020-01-22" "2020-01-23" "2020-01-24" "2020-01-25" [26] "2020-01-26" "2020-01-27" "2020-01-28" "2020-01-29" "2020-01-30" [31] "2020-01-31" "2020-02-01" "2020-02-02" "2020-02-03" "2020-02-04" [36] "2020-02-05" "2020-02-06" "2020-02-07" "2020-02-08" "2020-02-09" [41] "2020-02-10" "2020-02-11" "2020-02-12" "2020-02-13" "2020-02-14" [46] "2020-02-15" "2020-02-16" "2020-02-17" "2020-02-18" "2020-02-19" [51] "2020-02-20" "2020-02-21" "2020-02-22" "2020-02-23" "2020-02-24" [56] "2020-02-25" "2020-02-26" "2020-02-27" "2020-02-28" > V1<-seq(as.Date("2020-02-28"), as.Date("2020-01-01"), by="days") Error in seq.int(0, to0 - from, by) : wrong sign in 'by' argument
在这里,由于第一个日期在第二个日期之后,因此出现“错误输入'by'参数”错误。
让我们看看更多示例-
> V2<-seq(as.Date("2020-01-01"), as.Date("2020-02-29"), by="days") > V2 [1] "2020-01-01" "2020-01-02" "2020-01-03" "2020-01-04" "2020-01-05" [6] "2020-01-06" "2020-01-07" "2020-01-08" "2020-01-09" "2020-01-10" [11] "2020-01-11" "2020-01-12" "2020-01-13" "2020-01-14" "2020-01-15" [16] "2020-01-16" "2020-01-17" "2020-01-18" "2020-01-19" "2020-01-20" [21] "2020-01-21" "2020-01-22" "2020-01-23" "2020-01-24" "2020-01-25" [26] "2020-01-26" "2020-01-27" "2020-01-28" "2020-01-29" "2020-01-30" [31] "2020-01-31" "2020-02-01" "2020-02-02" "2020-02-03" "2020-02-04" [36] "2020-02-05" "2020-02-06" "2020-02-07" "2020-02-08" "2020-02-09" [41] "2020-02-10" "2020-02-11" "2020-02-12" "2020-02-13" "2020-02-14" [46] "2020-02-15" "2020-02-16" "2020-02-17" "2020-02-18" "2020-02-19" [51] "2020-02-20" "2020-02-21" "2020-02-22" "2020-02-23" "2020-02-24" [56] "2020-02-25" "2020-02-26" "2020-02-27" "2020-02-28" "2020-02-29" > V2<-seq(as.Date("2020-01-01"), as.Date("2020-02-30"), by="days") Error in charToDate(x) : character string is not in a standard unambiguous format > V2<-seq(as.Date("2020-01-01"), as.Date("2030-02-30"), by="days") Error in charToDate(x) : character string is not in a standard unambiguous format
在这里,错误表示日期2020-02-30和2030-02-30不存在,因为那是2月。
> V3<-seq(as.Date("2020-06-01"), as.Date("2020-08-01"), by="days") > V3 [1] "2020-06-01" "2020-06-02" "2020-06-03" "2020-06-04" "2020-06-05" [6] "2020-06-06" "2020-06-07" "2020-06-08" "2020-06-09" "2020-06-10" [11] "2020-06-11" "2020-06-12" "2020-06-13" "2020-06-14" "2020-06-15" [16] "2020-06-16" "2020-06-17" "2020-06-18" "2020-06-19" "2020-06-20" [21] "2020-06-21" "2020-06-22" "2020-06-23" "2020-06-24" "2020-06-25" [26] "2020-06-26" "2020-06-27" "2020-06-28" "2020-06-29" "2020-06-30" [31] "2020-07-01" "2020-07-02" "2020-07-03" "2020-07-04" "2020-07-05" [36] "2020-07-06" "2020-07-07" "2020-07-08" "2020-07-09" "2020-07-10" [41] "2020-07-11" "2020-07-12" "2020-07-13" "2020-07-14" "2020-07-15" [46] "2020-07-16" "2020-07-17" "2020-07-18" "2020-07-19" "2020-07-20" [51] "2020-07-21" "2020-07-22" "2020-07-23" "2020-07-24" "2020-07-25" [56] "2020-07-26" "2020-07-27" "2020-07-28" "2020-07-29" "2020-07-30" [61] "2020-07-31" "2020-08-01"