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.
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.
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.
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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With