R lubridate: How To Efficiently Work With Dates and Times in R

[This article was first published on Tag: r - Appsilon | Enterprise R Shiny Dashboards, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Data analytics and programming don’t often have a lot of things in common. But where they’re almost indistinguishable is the topic of dates. The skill of working with datetimes is an integral part of both areas and is something you’ll want to master sooner rather than later.

Newcomers often find this area challenging due to the sheer amount of options available, but the truth is that you’ll only need 5% of the functionality on a daily basis, and the rest you can quickly find online. Today, we’ll show you this 5% through a set of 15 useful examples with R lubridate.

But first, what is R lubridate, and what makes it better than the competition? Let’s answer that next.

Functional programming in R? Here’s everything you need to know.

Table of contents:

Why R Lubridate?

You can think of R lubridate as a comprehensive R package specifically designed to simplify the working process with dates and times. It’s part of tidyverse, so it aligns nicely with the philosophy of clear and expressive coding.

The package brings to the table a set of intuitive functions that make it easy for developers and data professionals to perform common tasks such as parsing dates, performing date arithmetic, and handling time zones.

One of the main reasons you want to consider using lubridate over the other datetime packages in R is its user-friendly and intuitive syntax. Traditional R functions are often verbose and complex, and lubridate is here to simplify things. For this reason, lubridate is also appealing to beginners and those who care about code simplicity and maintainability.

The rest of the article assumes you have lubridate installed and loaded. Use the following command to import it:


Up next, we’ll go over 15 concrete and useful examples so you can see why lubridate is so heavily used and loved by data professionals.

15 Examples to Master R Lubridate

This section will get you started working with R lubridate. It doesn’t matter if you’re a newcomer to R and programming in general – the examples will be simple to follow and easy to understand.

1. Get the Current Date and Time

You can use the now() function in lubridate to get the current date and time based on your system’s time zone. The function doesn’t require any parameters.

It is particularly useful when you need a timestamp for the exact moment when your script is running, such as logging events, timestamping transactions, or creating time-sensitive data entries:

current_time <- now()
Image 1 - Getting the current date and time

Image 1 – Getting the current date and time

2. Extract Date Components

The day(), month(), and year() functions are handy for extracting the respective date components from date objects. Each function expects a date or datetime object as the input.

These functions are great for breaking down a date into its constituent parts, which is useful for analysis, reporting, and data processing tasks where you need to categorize or compare data based on these individual components:

Image 2 - Extracting components from a date

Image 2 – Extracting components from a date

3. Parse Date From Character Strings

You can use the ymd(), mdy(), and dmy() functions to parse character strings into date objects. They expect the format to be year-month-day, month-day-year, and day-month-year, as the function name suggests.

They are crucial for converting date strings from various sources into a standardized date format that R can understand and manipulate:

Image 3 - Parsing date from a character string

Image 3 – Parsing date from a character string

4. Convert to Datetime From Other Formats

The as_datetime() function is used to convert different date and time formats into POSIXct, a common datetime format in R. It accepts a wide range of inputs like character strings, numeric types, and factors.

This function is invaluable for standardizing datetime information into a format suitable for analysis and keeping track of your data over time in general:

as_datetime("2023-11-13 17:34:51")
Image 4 - Converting string to datetime

Image 4 – Converting string to datetime

5. Find the Difference Between Dates

Subtracting two date objects in lubridate gives you the difference in time between them. This is a handy operation you’ll do pretty often and it doesn’t require a specific function – just subtract two dates the same way you would with numbers.

You can use this technique in many areas like project planning, age calculation, and time series analysis:

start_date <- ymd("20231101")
end_date <- ymd("20231113")
time_difference <- end_date - start_date
Image 5 - Finding difference between dates

Image 5 – Finding difference between dates

6. Add or Subtract Time Units

The days(), months() and years() functions can be used to create Period objects representing a specified number of days, months, or years. These can be added to or subtracted from date objects.

A practical application of these functions would be calculating future dates (e.g., deadlines or anniversaries) or past dates (e.g., historical analysis):

future_date <- now() + days(5)

past_date <- now() - months(3)
Image 6 - Adding and subtracting from dates

Image 6 – Adding and subtracting from dates

7. Compare Dates

Date objects in R can be compared using standard comparison operators like < or >. This doesn’t require a specific lubridate function – just compare the dates exactly the same as you would numbers.

Date comparison is essential for sorting, filtering, or conditional operations based on dates:

ymd("20231101") < ymd("20231029") ymd("20231101") > ymd("20231029")
Image 7 - Comparing dates

Image 7 – Comparing dates

8. Round Dates to the Nearest Unit

The round_date() function, as the name suggests, rounds a date to the specified time unit, such as “month” or “year”.

This function is useful when you need to normalize dates for comparison or aggregation, like summarizing data on a monthly or yearly basis:

round_date(now(), "month")
Image 8 - Rounding dates

Image 8 – Rounding dates

9. Working with Time Zones

You can use the with_tz() function to change the time zone of a datetime object without changing the actual time. It expects a datetime object and a string representing the time zone.

This function is crucial for handling data collected across different time zones, and ensures consistent and accurate time representations:

with_tz(now(), tzone = "UTC")
with_tz(now(), tzone = "EST")
Image 9 - Working with time zones

Image 9 – Working with time zones

10. Calculate Age from a Birthdate

If you want a practical use case of different lubridate functions, look no more. You can combine the interval() and years(1) to calculate the age from a birthdate by creating a time interval and dividing it by one year.

These calculations are widespread in demographics, marketing, and other fields where age is a significant factor, especially when represented as a floating point number:

birthdate <- ymd("1985-12-15")
age <- interval(birthdate, now()) / years(1)
Image 10 - Calculating age as a decimal number

Image 10 – Calculating age as a decimal number

11. Format Date Output

The format() function in R isn’t specific to lubridate, but allows you to format date or POSIXct objects into character strings in different formats.

This function is highly useful for preparing date-time data for reports, presentations, or exporting to other systems:

format(now(), "%A, %d %B %Y")
Image 11 - Custom formatted date output

Image 11 – Custom formatted date output

12. Create a Date Sequence

The seq() function, when used with date objects, generates a sequence of dates. The function requires start and end dates and a step size (e.g., “days”). Think of this last parameter as an interval.

This function is useful for creating time series data, finding missing dates in your time series, and so on:

seq(ymd("20231001"), ymd("20231031"), by = "days")
Image 12 - Creating a sequence between two dates

Image 12 – Creating a sequence between two dates

13. Handle Leap Years

You can use the leap_year() function to check whether a year is a leap year or not. It’s nothing you can’t implement on your own with the modulus operator, but this function is still the more convenient way to get the same functionality.

This function is critical in calendar calculations, especially in applications that require precise date calculations, like age calculation or scheduling:

Image 13 - Checking for leap years

Image 13 – Checking for leap years

14. Work With Durations and Periods

The duration() and period() functions are used to create Duration and Period objects representing time spans. The first one represents exact time spans, while the second one respects calendar conventions.

These functions are useful for precise time calculations and for handling regular time spans in scheduling:

duration(60, "seconds")
period(2, "hours")
Image 14 - Durations and periods

Image 14 – Durations and periods

15. Extract Weekdays

The wday() function is used to extract a weekday from a given date object, either as an index integer or as a factor. Setting the optional label parameter to TRUE will give you the actual day name.

It’s more or less a convenience function used when you want to aggregate or analyze data based on the day of the week:

wday(now(), label = TRUE)
Image 15 - Extracting weekdays and numbers

Image 15 – Extracting weekdays and numbers

Summing up R Lubridate

And there you have it – a brain-friendly introduction to working with dates and times with R lubridate. We think these 15 examples will be enough for most of the date-related things you’ll want to do. Anything more complex will typically boil down to combining a couple of functions shown today.

What do you think of R lubridate? Is it the package you use daily to handle date and time manipulations? If so, what’s your most commonly used function? Make sure to let us know in the comment section below.

Coming to R from Excel? Here’s a couple of advanced Excel-like functions in R for effective data manipulation.

The post appeared first on appsilon.com/blog/.

To leave a comment for the author, please follow the link and comment on their blog: Tag: r - Appsilon | Enterprise R Shiny Dashboards.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)