# 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 |

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.**

As you can see based on Table 1, our example data frame consists of a column with dates. Note that these dates where converted to date objects with the as.Date function.

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 |

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 |

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 |

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.

## Further Resources

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:

- weekdays, months, quarters & julian R Functions
- 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!

### Subscribe to my free statistics newsletter:

### R Tutorials

abs Function in R

all & any R Functions

Set Aspect Ratio of Plot

attach & detach R Functions

attr, attributes & structure in R

cbind R Command

Change ggplot2 Legend Title

Character to Numeric in R

Check if Object is Defined

col & row sums, means & medians

Complete Cases in R

Concatenate Vector of Strings

Convert Date to Weekday

cumsum R Function

Data Frame Column to Numeric

diff Command in R

difftime R Function

dim Function in R

dir R Function

Disable Scientific Notation

Draw Segments in R

droplevels R Example

Evaluate an Expression

Extract Characters from String

Factor to Numeric in R

Format Decimal Places

get, get0 & mget in R

is.na R Function

is.null Function in R

jitter R Function

Join Data with dplyr Package

length Function in R

lowess R Smoothing Function

max and min Functions in R

NA Omit in R

nchar R Function

ncol Function in R

nrow Function in R

outer Function in R

pairs & ggpairs Plot

parse, deparse & R expression

paste & paste0 Functions in R

pmax and pmin R Functions

polygon Plots in R

pretty R Function

R Find Missing Values

R Functions List (+ Examples)

R NA – Values

R Replace NA with 0

rbind & rbind.fill in R

Read Excel Files in R

readLines, n.readLines & readline

Remove Element from List

Remove Legend in ggplot2

Rename Column Name in R

Replace Last Comma of String

rev R Command

Round Numeric Data in R

Save & Load RData Workspace

scan R Function

setdiff R Function

setNames vs. setnames in R

sink Command in R

Sort, Order & Rank Data in R

sprintf Function in R

Square Root in R

str_c Function of stringr Package

str_sub Function of stringr Package

strptime & strftime Functions

substr & substring R Commands

sweep R Function

Transform Data Frames

union Function in R

unlist in R

weekdays, months, quarters & julian in R

with & within R Functions

Write Excel File in R