Date-Time Wrangling for Data Analysis in R
- Bernard Kilonzo

- Sep 3
- 4 min read

Overview
Date functions in R are essential for managing and analyzing time-related data, offering robust tools through both base R and packages like lubridate. R supports three primary date/time classes - Date for simple dates, and POSIXct and POSIXlt for date-time objects with time zones and components. You can create dates using as.Date(), ISOdate(), or strptime(), and manipulate them with arithmetic operations, extract components like year or hour, and format them using format(). The lubridate package streamlines parsing, rounding, and working with durations and intervals, making it easier to handle complex time series or scheduling tasks. Whether you're generating sequences, calculating differences, or adjusting time zones, R provides a comprehensive and flexible framework for working with temporal data.
Examples of Date-Time Functions in R
In this article, I will explore some of the common date-time functions for data analysis with R.
Load the required packages and data sets.
# load libraries
library(tidyverse)
# load data
superstore<-read.csv("https://raw.githubusercontent.com/bernardkilonzo-rigor/dataviz/main/data/Sample%20-%20Superstore.csv")1. Converting to Dates from Other Formats
The as.Date() function is used to convert different date formats into a common date format in R.
Similarly, ymd(), dmy(), and mdy() are crucial for converting date strings into standardized date formats in R.
# converting Order Date to a date object
superstore$Order.Date<-dmy(superstore$Order.Date)
# converting Ship Date to a date object
superstore$Ship.Date<-as.Date(superstore$Ship.Date, format = "%d/%m/%Y")2. Computing the Current Date and Time
Using now() function from lubridate to get the current date and time based on your system's time zone.
Or using Sys.Date() and Sys.time() to get the current date and time based on the local system.
# using lubridate now() function
now()
# using Sys.Date() or Sys.time() functions
Sys.Date()
Sys.time()3. Extracting Date Components (Year, Month, Day)
The functions day(), month(), and year() are useful for extracting the respective date components from date objects.
# extracting the year, month, and day components from Order Date
year(superstore$Order.Date) # extracting years
month(superstore$Order.Date) # extracting months
day(superstore$Order.Date) # extracting days4. Adding/Subtracting Time Units
The lubridate package allows you to add or subtract a specific time unit from date objects as shown below.
# adding 1 year to the Order Date
superstore<-superstore%>%
mutate(date_year = Order.Date+years(1))
# adding 1 month to the Order Date
superstore<-superstore%>%
mutate(date_month = Order.Date+months(1))
# subtracting 3 months from the Order Date
superstore<-superstore%>%
mutate(data_month_3 =Order.Date-months(3))5. Computing Difference Between Dates
Similarly, you can compute the difference between date objects as shown below.
# computing difference between Order Date and Ship Date
superstore<-superstore%>%
mutate(difference = Ship.Date-Order.Date)
# computing difference between Order Date and Ship Date using specific time length
superstore<-superstore%>%
mutate(diffe_2 = time_length(interval(Order.Date,Ship.Date),"month"))6. Rounding Dates to the Nearest Unit
The round_date() function rounds a date object to the specified time unit such as “month”, or “year”.
# rounding Order Date to the nearest month
superstore<-superstore%>%
mutate(round_month =round_date(Order.Date,"month"))
# rounding Order Date to the nearest year
superstore<-superstore%>%
mutate(round_year =round_date(Order.Date,"year"))7. Extracting Weekdays and Months
The functions wday() and month() are used to extract weekdays and months respectively from date objects as an integer or a string as shown below.
Note: Setting the optional label parameter to TRUE gives you the actual day/month name.
# extracting weekdays and months as integers
# extracting weekdays from Order Date
superstore<-superstore%>%
mutate(weekday = wday(Order.Date, week_start = 1))
# extracting months from Order Date
superstore<-superstore%>%
mutate(month = month(Order.Date))
# extracting weekdays and months as strings
# extracting weekdays from Order Date
superstore<-superstore%>%
mutate(wkday = wday(Order.Date, label = TRUE, week_start = 1))
# extracting months from Order Date
superstore<-superstore%>%
mutate(mon = month(Order.Date, label = TRUE))8. Creating a Date Sequence
The seq() function in R is a versatile tool for generating sequences of numbers, dates, or other ordered data types. When used with date objects, it generates a sequence of dates as shown below.
# the function below creates a sequence between the two dates
seq(dmy(01012021),dmy(31122024),by ="days")9. Working with Time Zones
With lubridate you can use with_tz() function to change the time zone of a datetime object without changing the actual time.
# converting my timezone to EST and UTC zones respectively
with_tz(now(), tzone = "EST")
with_tz(now(), tzone = "UTC")10. Formatting Date Outputs
The format() function in R is a powerful way to convert date and time objects into human-readable strings with custom formatting. Here are several practical examples to show its versatility:
# different ways of formatting the date "02-09-2025"
format(dmy(02092025),"%a") #"Tue"
format(dmy(02092025),"%A") #"Tuesday"
format(dmy(02092025),"%b") #"Sept"
format(dmy(02092025),"%B") #"September"
format(dmy(02092025),"%b-%Y") #"Sept-2025"
format(dmy(02092025),"%b-%y") #"Sept-25"
format(dmy(02092025),"%b %d,%Y") #"Sept 02,2025"Conclusion
Mastering date-time functions in R is a vital skill for anyone working with temporal data, whether in finance, epidemiology, environmental science, or everyday data wrangling. R’s native capabilities - anchored in the Date, POSIXct, and POSIXlt classes - offer a solid foundation for parsing, formatting, and manipulating dates and times. When paired with the intuitive power of the lubridate package, users gain access to a more human-readable and flexible toolkit for handling complex time-based operations, such as rounding, interval calculations, and time zone adjustments. From simple arithmetic to intricate scheduling logic, R empowers users to treat time not as a constraint but as a dimension of insight. By understanding and applying these functions effectively, analysts and developers can ensure their data workflows are both accurate and temporally aware - unlocking deeper patterns and more meaningful conclusions from their datasets.
If you like the work we do and would like to work with us, drop us an email on our contacts page and we’ll reach out!
Thank you for reading!!
