weekday name in R

How to get the weekday name from the date or number in R

There are multiple scenarios where you would like to get the weekday name from the date or number in R. Here are four solutions to get that depending on available content. The weekday number might already be in a separate column or obtainable from the date. It is not so easy as getting the month name from the month number in R by using built-in constants.

 

In this example, I will use the airquality dataset. To get in this data frame a column with dates, I will join separate year, month, and day components. I will also create a column with week-day numbers for the scenario when it is available.

data("airquality")

require(lubridate)


airquality$Date <- make_date(1973, airquality$Month, airquality$Day)
airquality$WD.Number <- wday(airquality$Date, week_start = 1)

head(airquality)

#  Ozone Solar.R Wind Temp Month Day       Date WD.Number
#1    41     190  7.4   67     5   1 1973-05-01         2
#2    36     118  8.0   72     5   2 1973-05-02         3
#3    12     149 12.6   74     5   3 1973-05-03         4
#4    18     313 11.5   62     5   4 1973-05-04         5
#5    NA      NA 14.3   56     5   5 1973-05-05         6
#6    28      NA 14.9   66     5   6 1973-05-06         7

Not all of the columns are necessary. I will drop a few of them.

airquality <- airquality[, -c(1:3)]

 

Get weekday name from date in R

One of the easiest ways to get a weekday name from the date in R is by using the weekdays function to return a full and abbreviated name. It is a base function, and no additional packages are necessary.

airquality$wday_base <- weekdays(airquality$Date)

airquality$wday_base_abb <- weekdays(airquality$Date, abbreviate = TRUE)

head(airquality)

#  Temp Month Day       Date WD.Number wday_base wday_base_abb
#1   67     5   1 1973-05-01         2   Tuesday           Tue
#2   72     5   2 1973-05-02         3 Wednesday           Wed
#3   74     5   3 1973-05-03         4  Thursday           Thu
#4   62     5   4 1973-05-04         5    Friday           Fri
#5   56     5   5 1973-05-05         6  Saturday           Sat
#6   66     5   6 1973-05-06         7    Sunday           Sun

 

For me, the results are in English. If you want to change your locale in R to get the month name in the desired language, you can do that like this.

Sys.setlocale("LC_TIME", "English")

 

Convert weekday numbers to text in R

If you have preferences on what weekday names should be, you can use a list and week-day numbers like this.

weekday_names <- c("Mo", "Tu", "We", "Th", "Fr", "Sa", "Su")

airquality$wday_const <- weekday_names[airquality$WD.Number]

head(airquality)


#  Temp Month Day       Date WD.Number wday_base wday_base_abb wday_const
#1   67     5   1 1973-05-01         2   Tuesday           Tue         Tu
#2   72     5   2 1973-05-02         3 Wednesday           Wed         We
#3   74     5   3 1973-05-03         4  Thursday           Thu         Th
#4   62     5   4 1973-05-04         5    Friday           Fri         Fr
#5   56     5   5 1973-05-05         6  Saturday           Sat         Sa
#6   66     5   6 1973-05-06         7    Sunday           Sun         Su

 

Other ways to get day of the week name from date in R

You can use a format function to get the weekday name if the dataset has a date column.

airquality$wday_format <- format(airquality$Date, "%a")

head(airquality)

#  Temp Month Day       Date WD.Number wday_base wday_base_abb wday_const wday_format
#1   67     5   1 1973-05-01         2   Tuesday           Tue         Tu         Tue
#2   72     5   2 1973-05-02         3 Wednesday           Wed         We         Wed
#3   74     5   3 1973-05-03         4  Thursday           Thu         Th         Thu
#4   62     5   4 1973-05-04         5    Friday           Fri         Fr         Fri
#5   56     5   5 1973-05-05         6  Saturday           Sat         Sa         Sat
#6   66     5   6 1973-05-06         7    Sunday           Sun         Su         Sun

Try to use capital A if you want to get the full weekday name.

Here is a good cheat sheet that contains date formatting options in R that might be useful in other situations.

 

Weekday name with the lubridate

The advantage of the lubridate function wday is an option to specify the locale. For example, if you want to get month names in German, it looks like this.

airquality$wday_lub <-
  wday(airquality$Date,
       label  = TRUE,
       abbr = TRUE,
       locale = "German")

head(airquality[, 4:ncol(airquality)])

#        Date WD.Number wday_base wday_base_abb wday_const wday_format wday_lub
#1 1973-05-01         2   Tuesday           Tue         Tu         Tue       Di
#2 1973-05-02         3 Wednesday           Wed         We         Wed       Mi
#3 1973-05-03         4  Thursday           Thu         Th         Thu       Do
#4 1973-05-04         5    Friday           Fri         Fr         Fri       Fr
#5 1973-05-05         6  Saturday           Sat         Sa         Sat       Sa
#6 1973-05-06         7    Sunday           Sun         Su         Sun       So

 

Here is how to get the month name from the month number in R.