Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Unique Sequence of Dates

Tags:

date

r

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.

like image 738
ATMathew Avatar asked Sep 24 '11 01:09

ATMathew


People also ask

How do you create a date sequence?

To create a sequence of dates we can leverage the seq() function. As with numeric vectors, you have to specify at least three of the four arguments ( from , to , by , and length. out ). Using the lubridate package is very similar.

How do I create a date sequence in Excel?

Use the Fill Handle Select the cell that contains the first date. Drag the fill handle across the adjacent cells that you want to fill with sequential dates. at the lower-right corner of the cell, hold down, and drag to fill the rest of the series. Fill handles can be dragged up, down, or across a spreadsheet.

What is date sequence?

Sequential dates occur when the digits on the date are in an increasing or decreasing sequence. Sequential dates depend on the format used to write the date - a date can only be sequential if only the last two digits of the year (YY) are used and tend to happen in the early part of a century.


1 Answers

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"
like image 76
joran Avatar answered Oct 26 '22 07:10

joran