How to create a sequence of dates by using starting date in R?

R ProgrammingServer Side ProgrammingProgramming

The best way to create a sequence of anything is creating it with the help of seq function and this also applies to sequences of dates. But in case of dates, we need to read the dates in date format so that R can understand the input type and create the appropriate vector. If we do not use the date format for the date value then it won’t make sense to R and it will result in error.

Examples

 Live Demo

x1<-seq(as.Date("2020-01-01"),by="day",length.out=5)
x1

Output

[1] "2020-01-01" "2020-01-02" "2020-01-03" "2020-01-04" "2020-01-05"

Examples

 Live Demo

x2<-seq(as.Date("2020-02-01"),by="day",length.out=29)
x2

Output

[1]  "2020-02-01" "2020-02-02" "2020-02-03" "2020-02-04" "2020-02-05"
[6]  "2020-02-06" "2020-02-07" "2020-02-08" "2020-02-09" "2020-02-10"
[11] "2020-02-11" "2020-02-12" "2020- 02-13" "2020-02-14" "2020-02-15"
[16] "2020-02-16" "2020-02-17" "2020-02-18" "2020-02-19" "2020-02-20"
[21] "2020-02-21" "2020-02-22" "2020-02-23" "2020-02-24" "2020-02-25"
[26] "2020-02-26" "2020-02-27" "2020-02-28" "2020-02-29"

Examples

 Live Demo

x3<-seq(as.Date("2020-02-01"),by="day",length.out=30)
x3

Output

[1]  "2020-02-01" "2020-02-02" "2020-02-03" "2020-02-04" "2020-02-05"
[6]  "2020-02-06" "2020-02-07" "2020-02-08" "2020-02-09" "2020-02-10"
[11] "2020-02-11" "2020-02-12" "2020-02-13" "2020-02-14" "2020-02-15"
[16] "2020-02-16" "2020-02-17" "2020-02-18" "2020-02-19" "2020-02-20"
[21] "2020-02-21" "2020-02-22" "2020-02-23" "2020-02-24" "2020-02-25"
[26] "2020-02-26" "2020-02-27" "2020-02-28" "2020-02-29" "2020-03-01"

Examples

 Live Demo

x4<-seq(as.Date("2020-03-01"),by="day",length.out=31)
x4

Output

[1] "2020-03-01" "2020-03-02" "2020-03-03" "2020-03-04" "2020-03-05"
[6] "2020-03-06" "2020-03-07" "2020-03-08" "2020-03-09" "2020-03-10"
[11] "2020-03-11" "2020-03-12" "2020-03-13" "2020-03-14" "2020-03-15"
[16] "2020-03-16" "2020-03-17" "2020-03-18" "2020-03-19" "2020-03-20"
[21] "2020-03-21" "2020-03-22" "2020-03-23" "2020-03-24" "2020-03-25"
[26] "2020-03-26" "2020-03-27" "2020-03-28" "2020-03-29" "2020-03-30"
[31] "2020-03-31"

Examples

 Live Demo

x5<-seq(as.Date("2020-04-01"),by="day",length.out=60)
x5

Output

[1] "2020-04-01" "2020-04-02" "2020-04-03" "2020-04-04" "2020-04-05"
[6] "2020-04-06" "2020-04-07" "2020-04-08" "2020-04-09" "2020-04-10"
[11] "2020-04-11" "2020-04-12" "2020-04-13" "2020-04-14" "2020-04-15"
[16] "2020-04-16" "2020-04-17" "2020-04-18" "2020-04-19" "2020-04-20"
[21] "2020-04-21" "2020-04-22" "2020-04-23" "2020-04-24" "2020-04-25"
[26] "2020-04-26" "2020-04-27" "2020-04-28" "2020-04-29" "2020-04-30"
[31] "2020-05-01" "2020-05-02" "2020-05-03" "2020-05-04" "2020-05-05"
[36] "2020-05-06" "2020-05-07" "2020-05-08" "2020-05-09" "2020-05-10"
[41] "2020-05-11" "2020-05-12" "2020-05-13" "2020-05-14" "2020-05-15"
[46] "2020-05-16" "2020-05-17" "2020-05-18" "2020-05-19" "2020-05-20"
[51] "2020-05-21" "2020-05-22" "2020-05-23" "2020-05-24" "2020-05-25"
[56] "2020-05-26" "2020-05-27" "2020-05-28" "2020-05-29" "2020-05-30"

Examples

 Live Demo

x6<-seq(as.Date("2020-05-15"),by="day",length.out=30)
x6

Output

[1] "2020-05-15" "2020-05-16" "2020-05-17" "2020-05-18" "2020-05-19"
[6] "2020-05-20" "2020-05-21" "2020-05-22" "2020-05-23" "2020-05-24"
[11] "2020-05-25" "2020-05-26" "2020-05-27" "2020-05-28" "2020-05-29"
[16] "2020-05-30" "2020-05-31" "2020-06-01" "2020-06-02" "2020-06-03"
[21] "2020-06-04" "2020-06-05" "2020-06-06" "2020-06-07" "2020-06-08"
[26] "2020-06-09" "2020-06-10" "2020-06-11" "2020-06-12" "2020-06-13"

Examples

 Live Demo

x7<-seq(as.Date("2020-05-15"),by="day",length.out=15)
x7

Output

[1] "2020-05-15" "2020-05-16" "2020-05-17" "2020-05-18" "2020-05-19"
[6] "2020-05-20" "2020-05-21" "2020-05-22" "2020-05-23" "2020-05-24"
[11] "2020-05-25" "2020-05-26" "2020-05-27" "2020-05-28" "2020-05-29"

Examples

 Live Demo

x8<-seq(as.Date("2020-06-14"),by="day",length.out=31)
x8

Output

[1] "2020-06-14" "2020-06-15" "2020-06-16" "2020-06-17" "2020-06-18"
[6] "2020-06-19" "2020-06-20" "2020-06-21" "2020-06-22" "2020-06-23"
[11] "2020-06-24" "2020-06-25" "2020-06-26" "2020-06-27" "2020-06-28"
[16] "2020-06-29" "2020-06-30" "2020-07-01" "2020-07-02" "2020-07-03"
[21] "2020-07-04" "2020-07-05" "2020-07-06" "2020-07-07" "2020-07-08"
[26] "2020-07-09" "2020-07-10" "2020-07-11" "2020-07-12" "2020-07-13"
[31] "2020-07-14"

Examples

 Live Demo

x9<-seq(as.Date("2020-11-01"),by="day",length.out=30)
x9

Output

[1] "2020-11-01" "2020-11-02" "2020-11-03" "2020-11-04" "2020-11-05"
[6] "2020-11-06" "2020-11-07" "2020-11-08" "2020-11-09" "2020-11-10"
[11] "2020-11-11" "2020-11-12" "2020-11-13" "2020-11-14" "2020-11-15"
[16] "2020-11-16" "2020-11-17" "2020-11-18" "2020-11-19" "2020-11-20"
[21] "2020-11-21" "2020-11-22" "2020-11-23" "2020-11-24" "2020-11-25"
[26] "2020-11-26" "2020-11-27" "2020-11-28" "2020-11-29" "2020-11-30"

Examples

 Live Demo

x10<-seq(as.Date("2020-12-01"),by="day",length.out=62)
x10

Output

[1] "2020-12-01" "2020-12-02" "2020-12-03" "2020-12-04" "2020-12-05"
[6] "2020-12-06" "2020-12-07" "2020-12-08" "2020-12-09" "2020-12-10"
[11] "2020-12-11" "2020-12-12" "2020-12-13" "2020-12-14" "2020-12-15"
[16] "2020-12-16" "2020-12-17" "2020-12-18" "2020-12-19" "2020-12-20"
[21] "2020-12-21" "2020-12-22" "2020-12-23" "2020-12-24" "2020-12-25"
[26] "2020-12-26" "2020-12-27" "2020-12-28" "2020-12-29" "2020-12-30"
[31] "2020-12-31" "2021-01-01" "2021-01-02" "2021-01-03" "2021-01-04"
[36] "2021-01-05" "2021-01-06" "2021-01-07" "2021-01-08" "2021-01-09"
[41] "2021-01-10" "2021-01-11" "2021-01-12" "2021-01-13" "2021-01-14"
[46] "2021-01-15" "2021-01-16" "2021-01-17" "2021-01-18" "2021-01-19"
[51] "2021-01-20" "2021-01-21" "2021-01-22" "2021-01-23" "2021-01-24"
[56] "2021-01-25" "2021-01-26" "2021-01-27" "2021-01-28" "2021-01-29"
[61] "2021-01-30" "2021-01-31"
raja
Published on 08-Oct-2020 14:10:48
Advertisements