How can I find the day of the week in R? Can you provide some examples?

How can I find the day of the week in R? Can you provide some examples?

To find the day of the week in R, one can use the built-in function “weekdays()” which returns the day of the week for a given date. This function can be applied to both single dates as well as vectors of dates. For example, if we have a date vector “dates” containing multiple dates, we can use the code “weekdays(dates)” to get the corresponding days of the week for each date. Additionally, one can also use the “format()” function to specify the format in which the day of the week is displayed. For example, “format(dates, “%A”)” would return the day of the week in the full name format (e.g. Monday, Tuesday, etc.). By utilizing these functions, one can easily find the day of the week in R for any given date.

Find Day of the Week in R (With Examples)


You can use the following functions from the 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 .

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

Cite this article

stats writer (2024). How can I find the day of the week in R? Can you provide some examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-find-the-day-of-the-week-in-r-can-you-provide-some-examples/

stats writer. "How can I find the day of the week in R? Can you provide some examples?." PSYCHOLOGICAL SCALES, 12 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-find-the-day-of-the-week-in-r-can-you-provide-some-examples/.

stats writer. "How can I find the day of the week in R? Can you provide some examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-find-the-day-of-the-week-in-r-can-you-provide-some-examples/.

stats writer (2024) 'How can I find the day of the week in R? Can you provide some examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-find-the-day-of-the-week-in-r-can-you-provide-some-examples/.

[1] stats writer, "How can I find the day of the week in R? Can you provide some examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I find the day of the week in R? Can you provide some examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top