Hi I'm trying to get a sequence of dates with lubridate
This doesn't work
seq(ymd('2012-04-07'),ymd('2013-03-22'),by=week(1))
the base command
seq(as.Date('2012-04-7'),as.Date('2013-03-22'),'weeks')
does, but I'd like to know if there is an elegant way to do this with lubridate.
EDIT
Please ignore : solved myself so leaving up for posterity only. Happy to have this deleted if necessary.
seq(ymd('2012-04-07'),ymd('2013-03-22'),by='weeks')
Does the trick
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 ).
Lubridate is an R package that makes it easier to work with dates and times. Below is a concise tour of some of the things lubridate can do for you. Lubridate was created by Garrett Grolemund and Hadley Wickham, and is now maintained by Vitalie Spinu.
POSIXct stores both a date and time with an associated time zone. The default time zone selected, is the time zone that your computer is set to which is most often your local time zone. POSIXct stores date and time in seconds with the number of seconds beginning at 1 January 1970.
lubridate: Make Dealing with Dates a Little EasierFunctions to work with date-times and time-spans: fast and user friendly parsing of date-time data, extraction and updating of components of a date-time (years, months, days, hours, minutes, and seconds), algebraic manipulation on date-time and time-span objects.
ymd
is a wrapper to parse date strings and returns a POSIXct
object.
You simply need to use standard terminology described in ?seq.POSIXt
(not lubridate
) to define weeks
seq(ymd('2012-04-07'),ymd('2013-03-22'), by = '1 week') seq(ymd('2012-04-07'),ymd('2013-03-22'), by = 'weeks')
will works
as will
seq(ymd('2012-04-07'),ymd('2013-03-22'), by = '2 week')
You could coerce the lubridate
Period
class object to a difftime
, but that seems rather unnecessary
seq(ymd('2012-04-07'),ymd('2013-03-22'), by = as.difftime(weeks(1)))
This is a way to stick within the POSIXct
universe of lubridate
and not change date formats to base R's POSIXt
. I avoid changing the date format in my scripts because I find it is a common place where bugs (for example time-zone changes or losing timestamps) are introduced. It follows this advice to use %m+%
: R: adding 1 month to a date
# example date is a leap day for a "worst case scenario" library("lubridate") posixct.in <- parse_date_time(x = "2016-02-29", orders = "ymd") # [1] "2016-02-29 UTC" posixct.seq <- posixct.in %m+% years(x = seq.int(from = 0, to = 3, by = 1)) # [1] "2016-02-29 UTC" "2017-02-28 UTC" "2018-02-28 UTC" "2019-02-28 UTC" posixct.seq <- posixct.in %m+% months(x = seq.int(from = 0, to = 3, by = 1)) # [1] "2016-02-29 UTC" "2016-03-29 UTC" "2016-04-29 UTC" "2016-05-29 UTC" posixct.seq <- posixct.in %m+% days(x = seq.int(from = 0, to = 3, by = 1)) # [1] "2016-02-29 UTC" "2016-03-01 UTC" "2016-03-02 UTC" "2016-03-03 UTC" posixct.seq <- posixct.in %m+% weeks(x = seq.int(from = 0, to = 3, by = 1)) # [1] "2016-02-29 UTC" "2016-03-07 UTC" "2016-03-14 UTC" "2016-03-21 UTC"
A regular +
also works sometimes, but the %m+%
prevents errors like this:
posixct.seq <- posixct.in + years(x = seq.int(from = 0, to = 3, by = 1)) # [1] "2016-02-29 UTC" NA NA NA
At first I was confused because I thought %m+
was just a way to add months, and similar lubridate
commands like %y+%
etc. do not exist. But, turns out the "m" doesn't stand for "month addition". My best guess is "magic" =)
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