Dates and times are essential components in many programming tasks, and R provides various functions and packages to handle them effectively. In this post, we’ll explore some common operations using both the base R functions and the lubridate package, comparing their simplicity and ease of understanding.
Let’s dive right in!
# What class does as.Date() produce? class(as.Date("1881/10/25"))
# be sure lubridate # install.packages("lubridate") library(lubridate) # Which do you find easier to understand? base or lubridate? today() # today() = Sys.Date()
now() # now() = Sys.time()
 "2023-05-16 08:27:52 EDT"
# as_date and as.Date produce the same class class(as_date("1881/10/25")) # lubridate
class(as.Date("1881/10/25")) # base
# simpler strptime strptime("2014-07-13 16:00:00 -0300", "%Y-%m-%d %H:%M:%S %z") # time zone is messed up
 "2014-07-13 15:00:00"
parse_date_time("2014-07-13 16:00:00 -0300", "ymd HMS z") # time zone works
 "2014-07-13 19:00:00 UTC"
# lubridate takes it one step further ymd("2014-07-13 16:00:00 -0300")
ymd_hms("2014-07-13 16:00:00 -0300")
 "2014-07-13 19:00:00 UTC"
mdy_hm("July 13, 2014 4:00 pm")
 "2014-07-13 16:00:00 UTC"
1️⃣ Determining the Class of a Date: The first line of code checks the class produced by the
as.Date() function when given the input “1881/10/25.” By using the
class() function, we can identify that the output is of class “Date.” This means that the
as.Date() function converts the input into a date format.
2️⃣ Base R vs. lubridate: Before we proceed further, we need to ensure that the lubridate package is installed. If not, the code installs it using the
install.packages() function. We then load the package using the
Next, we compare the ease of use between base R and lubridate for working with dates and times.
Today’s Date and Current Time: The
today()function, equivalent to
Sys.Date(), gives you the current date. Similarly,
now()returns the current date and time using
Sys.time(). These functions make it straightforward to obtain the current date or date and time in R.
Class Comparison: We compare the classes of dates produced by
as_date()from lubridate and
as.Date()from base R. Using the
class()function on each result, we observe that both functions produce the same “Date” class output. Hence, both methods are equivalent in this regard.
3️⃣ Simplifying Date and Time Parsing: Parsing date and time strings can sometimes be tricky, especially when dealing with time zones. However, lubridate provides simplified functions to handle such scenarios.
strptime()function is a base R function that parses a date and time string based on a given format. In this case, we try to parse “2014-07-13 16:00:00 -0300” with the format “%Y-%m-%d %H:%M:%S %z.” However, we encounter a problem with the time zone, as it does not parse correctly.
parse_date_time(): To overcome the time zone issue, lubridate offers the
parse_date_time()function. We provide the same date and time string along with the format “ymd HMS z.” This time, the time zone is parsed correctly, resulting in a valid date and time object.
4️⃣ Going the Extra Mile with lubridate: lubridate takes date and time manipulation a step further with its intuitive functions.
ymd()function converts a character string of the form “2014-07-13 16:00:00 -0300” into a date object. It handles various date formats and automatically infers the year, month, and day information.
ymd_hms(): Similar to
ymd_hms()function converts a character string into a date-time object, considering the year, month, day, hour, minute, and second components.
mdy_hm()function allows us to parse a character string like “July 13, 2014 4:00 pm” into
a date-time object. It handles different date formats and automatically extracts the month, day, year, hour, and minute information.
By leveraging these functions, lubridate simplifies the process of working with dates and times, offering a more intuitive and concise syntax compared to base R.
In conclusion, understanding how to handle dates and times in R is crucial for many programming tasks. While base R provides essential functions, the lubridate package offers additional capabilities and a more straightforward syntax, making it an attractive choice for working with dates and times in R.