Demystifying Dates: Finding the Day of the Week in R with lubridate

code
rtip
timeseries
Author

Steven P. Sanderson II, MPH

Published

February 9, 2024

Introduction

Have you ever stared at a date in R and wondered, “What day of the week was this?!” Fear not, fellow data wranglers! Today, we embark on a journey to conquer this seemingly simple, yet surprisingly tricky, task. Buckle up, because we’re about to become date whisperers with the help of the lubridate package.

The Power of lubridate

R’s built-in date functions are decent, but lubridate takes things to a whole new level. Think of it as a Swiss Army knife for everything date-related. It offers a wider range of functions, clear syntax, and handles different date formats like a champ.

Unveiling the Mystery: Extracting the Day of the Week

There are two main approaches to finding the day of the week in lubridate:

Example 1: Using wday()

This function is your go-to for both numeric and character representations of the day. Let’s break it down:

library(lubridate)

# Sample date
date <- ymd("2024-02-09")

# Numeric day (Monday = 1, Sunday = 7)
numeric_day <- wday(date)
print(numeric_day)  # Output: 6 (Friday)
[1] 6
class(numeric_day)
[1] "numeric"
# Character day (full name)
full_day <- wday(date, label = TRUE)
print(full_day)  # Output: Friday
[1] Fri
Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat
class(full_day)
[1] "ordered" "factor" 
# Character day (abbreviated)
abbrev_day <- wday(date, label = TRUE, abbr = TRUE)
print(abbrev_day)  # Output: Fri
[1] Fri
Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat
class(abbrev_day)
[1] "ordered" "factor" 

Example 2. Using strftime()

This function offers more flexibility in formatting dates, including extracting the day of the week.

# Same date as before
date <- ymd("2024-02-09")
class(date)
[1] "Date"
# Day of the week (full name)
full_day <- strftime(date, format = "%A")
print(full_day)  # Output: Friday
[1] "Friday"
class(full_day)
[1] "character"
# Day of the week (abbreviated)
abbrev_day <- strftime(date, format = "%a")
print(abbrev_day)  # Output: Fri
[1] "Fri"
class(abbrev_day)
[1] "character"

Beyond the Basics: Customizing Your Output

Both wday() and strftime() offer options to personalize your results. For example, you can change the starting day of the week (default is Monday) or use different formatting codes for the day name.

Bonus Tip: Check out the lubridate documentation for more advanced options and functionalities!

Time to Play!

Now it’s your turn to experiment! Here are some ideas:

  • Find the day of your birthday in R.
  • Analyze historical data and see how weekdays affect specific variables.
  • Create a calendar visualization with the day of the week displayed.

Remember, the more you practice, the more comfortable you’ll become with manipulating dates in R. So, dive in, explore, and have fun!

P.S. Don’t forget to share your creations and questions in the comments below. The R community is always happy to help!