How to create a vector with dates between two dates in R?

R ProgrammingServer Side ProgrammingProgramming

Create a vector with dates is not an easy task but with help of seq and as.Date it becomes easy in R. With the help of these functions we can create a vector in R that contain dates between two dates. But this cannot be done in reverse order, for example, if we want to have future date as first element of the vector then it would not be possible.

Example

> 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

Here, we are getting the error “wrong sign in ‘by’ argument” because the first date comes after second date.

Let’s have a look at some more examples −

> 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

Here, the error represents that date 2020-02-30 and 2030-02-30 does not exist because that is Feb.

> 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"
raja
Published on 12-Aug-2020 13:07:23
Advertisements