Skip to content

Instantly share code, notes, and snippets.

@fboehm
Last active January 24, 2017 20:53
Show Gist options
  • Save fboehm/c391f23cea0353f71386b88e50d5e9cb to your computer and use it in GitHub Desktop.
Save fboehm/c391f23cea0353f71386b88e50d5e9cb to your computer and use it in GitHub Desktop.
Get consecutive dates with R package `lubridate`

When working with dates, I sometimes have counts, say of tweets from a certain account, that don't occur every day. When analyzing these data, it would be helpful to have every date present in my analysis data set, even those dates for which the count is zero. For some time, I didn't know how to do this. I tried:

library(lubridate)
mdy("1/1/2015") -> foo
mdy("1/1/2016") -> bar
foo:bar

which outputted a collection of 366 (consecutive) integers.

I then tried:

mdy(foo:bar)

which outputted 366 NAs, with a message that no origin value was specified.

Now, to get 366 dates, you can use as_date from lubridate:

as_date(foo:bar)

to get the consecutive 366 dates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment