lubridate: working with date and time in R

Working with date and time in R is sometimes tricky. Today I gave lubridate a try and was surprised on how easy it can be. Lubridate is a available on git and on CRAN. There is also a good introduction published in the Journal of Statistical Software.

# Create an object
bday <- dmy("23121984")

This could also have been achieved with any combination of d(ay)m(onth)y(ear), i.e. ymd() or dym().

Several options are provided to work with the bday object:

wday(bday)  # day of the week
wday(bday, label=T)  # day of the week, abreviated
yday(bday)  # day of the year

lubridate also makes it easy to calculate with dates

wday(bday + years(1), label=T)  # day of week one year later

table(sapply(1:100, function(x) wday(bday + years(x), label=T)))  # days of the week for next 100 years.
About these ads
This entry was posted in R, Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s