How to Find Day of the Week in R (With Examples)


You can use the following functions from the lubridate package in R to quickly find the day of the week:

Method 1: Find Numeric Day of Week (Assuming Week Starts on Sunday)

wday(df$date_column)

Method 2: Find Numeric Day of Week (Assuming Week Starts on Monday)

wday(df$date_column, week_start=1)

Method 3: Find Character Day of Week (Using Abbreviated Labels)

wday(df$date_column, label=TRUE)

Method 4: Find Character Day of Week (Using Full Weekday Labels)

wday(df$date_column, label=TRUE, abbr=FALSE)

The following examples show how to use each method in practice with the following data frame:

library(lubridate)

#create data frame
df <- data.frame(date=c('2020-10-11', '2020-10-19', '2020-10-31'),
                 sales=c(435, 768, 945))

#view data frame
df

        date sales
1 2020-10-11   435
2 2020-10-19   768
3 2020-10-31   945

Method 1: Find Numeric Day of Week (Assuming Week Starts on Sunday)

The following code shows how to find the numeric day of the week of the values in the ‘date’ column:

#find day of week
df$weekday <- wday(df$date)

#view updated data frame
df

        date sales weekday
1 2020-10-11   435       1
2 2020-10-19   768       2
3 2020-10-31   945       7

Note that 1 indicates a Sunday, 2 indicates a Monday, and so on.

Method 2: Find Numeric Day of Week (Assuming Week Starts on Monday)

The following code shows how to find the numeric day of the week (assuming a week starts on Monday) of the values in the ‘date’ column:

#find day of week
df$weekday <- wday(df$date, week_start=1)

#view updated data frame
df

        date sales weekday
1 2020-10-11   435       7
2 2020-10-19   768       1
3 2020-10-31   945       6

In this scenario, a 1 indicates a Monday, 2 indicates a Tuesday, and so on.

Method 3: Find Character Day of Week (Using Abbreviated Labels)

The following code shows how to find the abbreviated character day of the week of the values in the ‘date’ column:

#find day of week
df$weekday <- wday(df$date, label=TRUE)

#view updated data frame
df

        date sales weekday
1 2020-10-11   435     Sun
2 2020-10-19   768     Mon
3 2020-10-31   945     Sat

Method 4: Find Character Day of Week (Using Full Weekday Labels)

The following code shows how to find the character day of the week (using full weekday labels) of the values in the ‘date’ column:

#find day of week
df$weekday <- wday(df$date, label=TRUE, abbr=FALSE)

#view updated data frame
df

        date sales  weekday
1 2020-10-11   435   Sunday
2 2020-10-19   768   Monday
3 2020-10-31   945 Saturday

Note: You can find the complete documentation for the lubridate wday() function here.

Additional Resources

The following tutorials explain how to perform other common operations with dates in R:

How to Extract Year from Date in R
How to Convert Date to Numeric in R
How to Sort a Data Frame by Date in R
How to Aggregate Daily Data to Monthly and Yearly in R

Leave a Reply

Your email address will not be published.