Create a unique date sequence

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

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

I want to know if this data can be generated with the seq() command.

Something like the following: (obviously not working)

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

Instead, I should use toDate and regex to create this date in this particular format.

+6
source share
4 answers

As I noted in my comment, seq has a 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" 
+19
source

I made the same mistake with seq () trying to make a numerical sequence. Do not use the ":" operator between arguments, and they will need to be dates if you want to create a sequence of dates. Another way is to take one date and add a numerical sequence starting from 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" 
+9
source

You can try timeBasedSeq in the xts package. Note that the argument is a string and the use of a double colon.

 timeBasedSeq("2011-08-01::2011-08-31") 
+7
source

I had to generate seq on min, and it was not easy, but I found seq.POSIXt in the database. In your case, you want to receive daily data from 2011-08-01 to 2011-08-31. Therefore I suggest you

 days <- seq(ISOdate(2011,8,1), by = "day", length.out = 31) class(days) "POSIXct" "POSIXt" 
+1
source

Source: https://habr.com/ru/post/897950/


All Articles