How to create a vector with all dates in a particular year in R?

R ProgrammingServer Side ProgrammingProgramming

We know that some years are leap years and some are normal years. The leap years have 366 days and the normal years have 365 days. To create a vector with all dates in a particular year we can use first date and the last date of the year by reading them with as.Date and creating a sequence with seq function. Check out the below examples to understand how it is done.

Example1

Creating a vector with dates in year 2020 −

 Live Demo

seq(as.Date("2020-01-01"),as.Date("2020-12-31"),by="1 day")

Output

[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"
[61] "2020−03−01" "2020−03−02" "2020−03−03" "2020−03−04" "2020−03−05"
[66] "2020−03−06" "2020−03−07" "2020−03−08" "2020−03−09" "2020−03−10"
[71] "2020−03−11" "2020−03−12" "2020−03−13" "2020−03−14" "2020−03−15"
[76] "2020−03−16" "2020−03−17" "2020−03−18" "2020−03−19" "2020−03−20"
[81] "2020−03−21" "2020−03−22" "2020−03−23" "2020−03−24" "2020−03−25"
[86] "2020−03−26" "2020−03−27" "2020−03−28" "2020−03−29" "2020−03−30"
[91] "2020−03−31" "2020−04−01" "2020−04−02" "2020−04−03" "2020−04−04"
[96] "2020−04−05" "2020−04−06" "2020−04−07" "2020−04−08" "2020−04−09"
[101] "2020−04−10" "2020−04−11" "2020−04−12" "2020−04−13" "2020−04−14"
[106] "2020−04−15" "2020−04−16" "2020−04−17" "2020−04−18" "2020−04−19"
[111] "2020−04−20" "2020−04−21" "2020−04−22" "2020−04−23" "2020−04−24"
[116] "2020−04−25" "2020−04−26" "2020−04−27" "2020−04−28" "2020−04−29"
[121] "2020−04−30" "2020−05−01" "2020−05−02" "2020−05−03" "2020−05−04"
[126] "2020−05−05" "2020−05−06" "2020−05−07" "2020−05−08" "2020−05−09"
[131] "2020−05−10" "2020−05−11" "2020−05−12" "2020−05−13" "2020−05−14"
[136] "2020−05−15" "2020−05−16" "2020−05−17" "2020−05−18" "2020−05−19"
[141] "2020−05−20" "2020−05−21" "2020−05−22" "2020−05−23" "2020−05−24"
[146] "2020−05−25" "2020−05−26" "2020−05−27" "2020−05−28" "2020−05−29"
[151] "2020−05−30" "2020−05−31" "2020−06−01" "2020−06−02" "2020−06−03"
[156] "2020−06−04" "2020−06−05" "2020−06−06" "2020−06−07" "2020−06−08"
[161] "2020−06−09" "2020−06−10" "2020−06−11" "2020−06−12" "2020−06−13"
[166] "2020−06−14" "2020−06−15" "2020−06−16" "2020−06−17" "2020−06−18"
[171] "2020−06−19" "2020−06−20" "2020−06−21" "2020−06−22" "2020−06−23"
[176] "2020−06−24" "2020−06−25" "2020−06−26" "2020−06−27" "2020−06−28"
[181] "2020−06−29" "2020−06−30" "2020−07−01" "2020−07−02" "2020−07−03"
[186] "2020−07−04" "2020−07−05" "2020−07−06" "2020−07−07" "2020−07−08"
[191] "2020−07−09" "2020−07−10" "2020−07−11" "2020−07−12" "2020−07−13"
[196] "2020−07−14" "2020−07−15" "2020−07−16" "2020−07−17" "2020−07−18"
[201] "2020−07−19" "2020−07−20" "2020−07−21" "2020−07−22" "2020−07−23"
[206] "2020−07−24" "2020−07−25" "2020−07−26" "2020−07−27" "2020−07−28"
[211] "2020−07−29" "2020−07−30" "2020−07−31" "2020−08−01" "2020−08−02"
[216] "2020−08−03" "2020−08−04" "2020−08−05" "2020−08−06" "2020−08−07"
[221] "2020−08−08" "2020−08−09" "2020−08−10" "2020−08−11" "2020−08−12"
[226] "2020−08−13" "2020−08−14" "2020−08−15" "2020−08−16" "2020−08−17"
[231] "2020−08−18" "2020−08−19" "2020−08−20" "2020−08−21" "2020−08−22"
[236] "2020−08−23" "2020−08−24" "2020−08−25" "2020−08−26" "2020−08−27"
[241] "2020−08−28" "2020−08−29" "2020−08−30" "2020−08−31" "2020−09−01"
[246] "2020−09−02" "2020−09−03" "2020−09−04" "2020−09−05" "2020−09−06"
[251] "2020−09−07" "2020−09−08" "2020−09−09" "2020−09−10" "2020−09−11"
[256] "2020−09−12" "2020−09−13" "2020−09−14" "2020−09−15" "2020−09−16"
[261] "2020−09−17" "2020−09−18" "2020−09−19" "2020−09−20" "2020−09−21"
[266] "2020−09−22" "2020−09−23" "2020−09−24" "2020−09−25" "2020−09−26"
[271] "2020−09−27" "2020−09−28" "2020−09−29" "2020−09−30" "2020−10−01"
[276] "2020−10−02" "2020−10−03" "2020−10−04" "2020−10−05" "2020−10−06"
[281] "2020−10−07" "2020−10−08" "2020−10−09" "2020−10−10" "2020−10−11"
[286] "2020−10−12" "2020−10−13" "2020−10−14" "2020−10−15" "2020−10−16"
[291] "2020−10−17" "2020−10−18" "2020−10−19" "2020−10−20" "2020−10−21"
[296] "2020−10−22" "2020−10−23" "2020−10−24" "2020−10−25" "2020−10−26"
[301] "2020−10−27" "2020−10−28" "2020−10−29" "2020−10−30" "2020−10−31"
[306] "2020−11−01" "2020−11−02" "2020−11−03" "2020−11−04" "2020−11−05"
[311] "2020−11−06" "2020−11−07" "2020−11−08" "2020−11−09" "2020−11−10"
[316] "2020−11−11" "2020−11−12" "2020−11−13" "2020−11−14" "2020−11−15"
[321] "2020−11−16" "2020−11−17" "2020−11−18" "2020−11−19" "2020−11−20"
[326] "2020−11−21" "2020−11−22" "2020−11−23" "2020−11−24" "2020−11−25"
[331] "2020−11−26" "2020−11−27" "2020−11−28" "2020−11−29" "2020−11−30"
[336] "2020−12−01" "2020−12−02" "2020−12−03" "2020−12−04" "2020−12−05"
[341] "2020−12−06" "2020−12−07" "2020−12−08" "2020−12−09" "2020−12−10"
[346] "2020−12−11" "2020−12−12" "2020−12−13" "2020−12−14" "2020−12−15"
[351] "2020−12−16" "2020−12−17" "2020−12−18" "2020−12−19" "2020−12−20"
[356] "2020−12−21" "2020−12−22" "2020−12−23" "2020−12−24" "2020−12−25"
[361] "2020−12−26" "2020−12−27" "2020−12−28" "2020−12−29" "2020−12−30"
[366] "2020−12−31"

Example2

Creating a vector with dates in year 2021 −

 Live Demo

seq(as.Date("2021-01-01"),as.Date("2021-12-31"),by="1 day")

Output

[1] "2021−01−01" "2021−01−02" "2021−01−03" "2021−01−04" "2021−01−05"
[6] "2021−01−06" "2021−01−07" "2021−01−08" "2021−01−09" "2021−01−10"
[11] "2021−01−11" "2021−01−12" "2021−01−13" "2021−01−14" "2021−01−15"
[16] "2021−01−16" "2021−01−17" "2021−01−18" "2021−01−19" "2021−01−20"
[21] "2021−01−21" "2021−01−22" "2021−01−23" "2021−01−24" "2021−01−25"
[26] "2021−01−26" "2021−01−27" "2021−01−28" "2021−01−29" "2021−01−30"
[31] "2021−01−31" "2021−02−01" "2021−02−02" "2021−02−03" "2021−02−04"
[36] "2021−02−05" "2021−02−06" "2021−02−07" "2021−02−08" "2021−02−09"
[41] "2021−02−10" "2021−02−11" "2021−02−12" "2021−02−13" "2021−02−14"
[46] "2021−02−15" "2021−02−16" "2021−02−17" "2021−02−18" "2021−02−19"
[51] "2021−02−20" "2021−02−21" "2021−02−22" "2021−02−23" "2021−02−24"
[56] "2021−02−25" "2021−02−26" "2021−02−27" "2021−02−28" "2021−03−01"
[61] "2021−03−02" "2021−03−03" "2021−03−04" "2021−03−05" "2021−03−06"
[66] "2021−03−07" "2021−03−08" "2021−03−09" "2021−03−10" "2021−03−11"
[71] "2021−03−12" "2021−03−13" "2021−03−14" "2021−03−15" "2021−03−16"
[76] "2021−03−17" "2021−03−18" "2021−03−19" "2021−03−20" "2021−03−21"
[81] "2021−03−22" "2021−03−23" "2021−03−24" "2021−03−25" "2021−03−26"
[86] "2021−03−27" "2021−03−28" "2021−03−29" "2021−03−30" "2021−03−31"
[91] "2021−04−01" "2021−04−02" "2021−04−03" "2021−04−04" "2021−04−05"
[96] "2021−04−06" "2021−04−07" "2021−04−08" "2021−04−09" "2021−04−10"
[101] "2021−04−11" "2021−04−12" "2021−04−13" "2021−04−14" "2021−04−15"
[106] "2021−04−16" "2021−04−17" "2021−04−18" "2021−04−19" "2021−04−20"
[111] "2021−04−21" "2021−04−22" "2021−04−23" "2021−04−24" "2021−04−25"
[116] "2021−04−26" "2021−04−27" "2021−04−28" "2021−04−29" "2021−04−30"
[121] "2021−05−01" "2021−05−02" "2021−05−03" "2021−05−04" "2021−05−05"
[126] "2021−05−06" "2021−05−07" "2021−05−08" "2021−05−09" "2021−05−10"
[131] "2021−05−11" "2021−05−12" "2021−05−13" "2021−05−14" "2021−05−15"
[136] "2021−05−16" "2021−05−17" "2021−05−18" "2021−05−19" "2021−05−20"
[141] "2021−05−21" "2021−05−22" "2021−05−23" "2021−05−24" "2021−05−25"
[146] "2021−05−26" "2021−05−27" "2021−05−28" "2021−05−29" "2021−05−30"
[151] "2021−05−31" "2021−06−01" "2021−06−02" "2021−06−03" "2021−06−04"
[156] "2021−06−05" "2021−06−06" "2021−06−07" "2021−06−08" "2021−06−09"
[161] "2021−06−10" "2021−06−11" "2021−06−12" "2021−06−13" "2021−06−14"
[166] "2021−06−15" "2021−06−16" "2021−06−17" "2021−06−18" "2021−06−19"
[171] "2021−06−20" "2021−06−21" "2021−06−22" "2021−06−23" "2021−06−24"
[176] "2021−06−25" "2021−06−26" "2021−06−27" "2021−06−28" "2021−06−29"
[181] "2021−06−30" "2021−07−01" "2021−07−02" "2021−07−03" "2021−07−04"
[186] "2021−07−05" "2021−07−06" "2021−07−07" "2021−07−08" "2021−07−09"
[191] "2021−07−10" "2021−07−11" "2021−07−12" "2021−07−13" "2021−07−14"
[196] "2021−07−15" "2021−07−16" "2021−07−17" "2021−07−18" "2021−07−19"
[201] "2021−07−20" "2021−07−21" "2021−07−22" "2021−07−23" "2021−07−24"
[206] "2021−07−25" "2021−07−26" "2021−07−27" "2021−07−28" "2021−07−29"
[211] "2021−07−30" "2021−07−31" "2021−08−01" "2021−08−02" "2021−08−03"
[216] "2021−08−04" "2021−08−05" "2021−08−06" "2021−08−07" "2021−08−08"
[221] "2021−08−09" "2021−08−10" "2021−08−11" "2021−08−12" "2021−08−13"
[226] "2021−08−14" "2021−08−15" "2021−08−16" "2021−08−17" "2021−08−18"
[231] "2021−08−19" "2021−08−20" "2021−08−21" "2021−08−22" "2021−08−23"
[236] "2021−08−24" "2021−08−25" "2021−08−26" "2021−08−27" "2021−08−28"
[241] "2021−08−29" "2021−08−30" "2021−08−31" "2021−09−01" "2021−09−02"
[246] "2021−09−03" "2021−09−04" "2021−09−05" "2021−09−06" "2021−09−07"
[251] "2021−09−08" "2021−09−09" "2021−09−10" "2021−09−11" "2021−09−12"
[256] "2021−09−13" "2021−09−14" "2021−09−15" "2021−09−16" "2021−09−17"
[261] "2021−09−18" "2021−09−19" "2021−09−20" "2021−09−21" "2021−09−22"
[266] "2021−09−23" "2021−09−24" "2021−09−25" "2021−09−26" "2021−09−27"
[271] "2021−09−28" "2021−09−29" "2021−09−30" "2021−10−01" "2021−10−02"
[276] "2021−10−03" "2021−10−04" "2021−10−05" "2021−10−06" "2021−10−07"
[281] "2021−10−08" "2021−10−09" "2021−10−10" "2021−10−11" "2021−10−12"
[286] "2021−10−13" "2021−10−14" "2021−10−15" "2021−10−16" "2021−10−17"
[291] "2021−10−18" "2021−10−19" "2021−10−20" "2021−10−21" "2021−10−22"
[296] "2021−10−23" "2021−10−24" "2021−10−25" "2021−10−26" "2021−10−27"
[301] "2021−10−28" "2021−10−29" "2021−10−30" "2021−10−31" "2021−11−01"
[306] "2021−11−02" "2021−11−03" "2021−11−04" "2021−11−05" "2021−11−06"
[311] "2021−11−07" "2021−11−08" "2021−11−09" "2021−11−10" "2021−11−11"
[316] "2021−11−12" "2021−11−13" "2021−11−14" "2021−11−15" "2021−11−16"
[321] "2021−11−17" "2021−11−18" "2021−11−19" "2021−11−20" "2021−11−21"
[326] "2021−11−22" "2021−11−23" "2021−11−24" "2021−11−25" "2021−11−26"
[331] "2021−11−27" "2021−11−28" "2021−11−29" "2021−11−30" "2021−12−01"
[336] "2021−12−02" "2021−12−03" "2021−12−04" "2021−12−05" "2021−12−06"
[341] "2021−12−07" "2021−12−08" "2021−12−09" "2021−12−10" "2021−12−11"
[346] "2021−12−12" "2021−12−13" "2021−12−14" "2021−12−15" "2021−12−16"
[351] "2021−12−17" "2021−12−18" "2021−12−19" "2021−12−20" "2021−12−21"
[356] "2021−12−22" "2021−12−23" "2021−12−24" "2021−12−25" "2021−12−26"
[361] "2021−12−27" "2021−12−28" "2021−12−29" "2021−12−30" "2021−12−31"
raja
Published on 05-Feb-2021 09:05:50
Advertisements