Convert Date to Day of Week in R (3 Examples) | How to Find the Weekday
In this article, I’ll explain how to find the weekday of a date in R. The tutorial is structured as follows
- Creation of Example Data in R
- Find Weekday with weekdays Function (Example 1)
- Find Weekday with strftime Function (Example 2)
- Find Weekday with as.POSIXlt Function (Example 3)
- Further Resources for the Handling of Dates
So without further ado, let’s dive into the examples…
Create Example Data
Before we can start with the examples, we have to create some example data that contains a date variable:
data <- data.frame(date = as.Date(c("2022-02-11", # Create example data "2010-12-01", "1955-02-08", "2005-01-01"))) data # Print example data to console
Table 1: Example Data Frame with Dates.
Now let’s move on to the examples, where we will find the weekdays that correspond to our dates…
Example 1: Convert Date to Weekday in R (weekdays Function)
In the first example, we are converting our dates to weekdays with the weekdays function. The function is explicitly designed to find the corresponding day of the week for a date object in R:
data1 <- data # Replicate data for Example 1 data1$weekday <- weekdays(data1$date) # Convert dates to weekdays data1 # Print converted data to console
Table 2: Example Data Frame with Dates & Weekdays.
As you can see in Table 2, we have just added a second variable to our data frame which contains the weekday for each of our dates.
Note: We were able to find the weekdays of dates in the past as well as of dates in the future (as in row 1).
The weekdays function is quite simple and easy to use. However, there are many other ways how to find weekdays and depending on your specific situation one of the following approaches might be better. So keep on reading…
Example 2: Convert Date to Weekday in R (strftime Function)
In the second example, we are using the strftime function:
data2 <- data # Replicate data for Example 2 data2$day <- strftime(data2$date, "%A") data2 # Print converted data to console # date weekday # 2022-02-11 Friday # 2010-12-01 Wednesday # 1955-02-08 Tuesday # 2005-01-01 Saturday
The output is exactly the same as in Example 1.
Looks good, so let’s move on to the next example…
Example 3: Convert Date to Weekday in R (as.POSIXlt Function)
In this example we are going to apply the as.POSIXlt command in order to find the corresponding days of the week:
data3 <- data # Replicate data for Example 3 data3$weekday <- c("Monday", "Tuesday", "Wednesday", # Convert dates to weekdays "Thursday", "Friday", "Saturday", "Sunday")[as.POSIXlt(data3$date)$wday] data3 # Print converted data to console # date weekday # 2022-02-11 Friday # 2010-12-01 Wednesday # 1955-02-08 Tuesday # 2005-01-01 Saturday
Again the same output. However, there is a big advantage of this approach: You can assign a name for each day of the week manually. For instance, this has advantages when your R or RStudio are set to a different language than English. In this case the approaches of Example 1 & 2 will assign the weekdays in a different language. With the approach of Example 3, you can assign any character string you want.
Handling dates in R is a difficult topic. For that reason I have listed some further resources about the handling of date objects in the following.
If you want to learn more about date objects in R, I can recommend the following YouTube video of Vincent King:
Also, you might have a look at the following R tutorials of statistical-programming.com:
- strptime & strftime R Functions
- The difftime R Function
- List of Useful R Functions
- The R Programming Language
I hope you found everything you were looking for in this tutorial. Let me know in the comments if you have any further questions. Of cause, general feedback is also very welcome!