Creating a unique sequence of dates
Asked Answered
S

6

20

Let's say that I want to generate a data frame which contains a column with is structured in the following format.

2011-08-01
2011-08-02
2011-08-03
2011-08-04
...

I want to know if it's possible to generate this data with the seq() command.

Something like the following: (obviously doesn't work)

seq(2011-08-01:2011-08-31)

Would I instead have to use toDate and regex to generate this date in this specific format.

Shanahan answered 24/9, 2011 at 1:10 Comment(2)
Try reading all the way down to the bottom of ?seq.Uprise
Note that if the purpose of this is to create a time series with those dates then you can avoid creating this sequence explicitly by creating a zooreg object. This example creates a "zooreg" class time series whose values are 1, 2, ..., 10 for 10 consecutive dates starting at 2011-08-01: library(zoo); z <- zooreg(1:10, as.Date("2011-08-01")) .Diep
U
46

As I noted in my comment, seq has method for dates, seq.Date:

seq(as.Date('2011-01-01'),as.Date('2011-01-31'),by = 1)
 [1] "2011-01-01" "2011-01-02" "2011-01-03" "2011-01-04" "2011-01-05" "2011-01-06" "2011-01-07" "2011-01-08"
 [9] "2011-01-09" "2011-01-10" "2011-01-11" "2011-01-12" "2011-01-13" "2011-01-14" "2011-01-15" "2011-01-16"
[17] "2011-01-17" "2011-01-18" "2011-01-19" "2011-01-20" "2011-01-21" "2011-01-22" "2011-01-23" "2011-01-24"
[25] "2011-01-25" "2011-01-26" "2011-01-27" "2011-01-28" "2011-01-29" "2011-01-30" "2011-01-31"
Uprise answered 24/9, 2011 at 1:29 Comment(1)
a bit more flexible way is seq(as.Date('2011-01-01'),as.Date('2011-01-31'),by = 'day') and one can generate seq by month or by year by using by = 'month' or any other intervalCommanding
W
12

I have made the same mistake with seq() attempting to make a numeric sequence. Don't use the ":" operator between arguments, and they will need to be dates if you want to make a date sequence. Another way is to take one date and add a numeric sequence starting with 0:

> as.Date("2000-01-01") + 0:10
 [1] "2000-01-01" "2000-01-02" "2000-01-03" "2000-01-04" "2000-01-05" "2000-01-06"
 [7] "2000-01-07" "2000-01-08" "2000-01-09" "2000-01-10" "2000-01-11"
Wyly answered 24/9, 2011 at 1:31 Comment(0)
T
9

You could try timeBasedSeq in the xts package. Notice the argument is a string and the use of the double-colon.

timeBasedSeq("2011-08-01::2011-08-31")
Temporal answered 24/9, 2011 at 1:52 Comment(0)
D
2

This worked for me! I know it's similar to the most upvoted answer, but that fact that i can specify "1 month" in text is amazing!

seq(as.Date("2012-01-01"),as.Date("2019-12-01"), by = "1 month")
Dearr answered 8/11, 2019 at 17:12 Comment(0)
S
1

I had to generate a seq by min, and it was no easy, but i found seq.POSIXt in base. In your case, you want a seq of daily data from 2011-08-01 to 2011-08-31. So i suggest you

days <- seq(ISOdate(2011,8,1), by = "day", length.out = 31)
class(days)
"POSIXct" "POSIXt"
Strickle answered 14/2, 2016 at 2:1 Comment(0)
F
0

You can use clock's date_build, which comes with nice functionalities and useful error message:

library(clock)
date_build(2011, 8, 1:31)
Furlana answered 3/10, 2022 at 14:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.