Skip to content
Related Articles

Related Articles

Improve Article

Convert Date to Day of Week in R

  • Last Updated : 23 May, 2021

In this article, we are going to see different approaches to convert date to day of the week in R Programming language.

The three different types of approaches to convert given dates to the day of the week in R language are as follows:

  • Converting date to day of the week using weekdays function
  • Converting date to day of the week using strftime function
  • Converting date to day of the week using as.POSIXlt function

Method 1: Using weekdays() function.

In this method we’re converting date to day of the week, the user needs to call the weekdays() function which is an in-build function in R language and pass the vectors of the date into it. By doing this it will be returning the weekdays of every given date to the user.

weekdays function: This function is used to extract the weekday, month or quarter, or the Julian time.



Syntax: weekdays(x, abbreviate)

Parameters:

  • x: an object inheriting from class “POSIXt” or “Date”.
  • abbreviate: logical vector (possibly recycled). Should the names be abbreviated?

Example:

In this example, we will be converting the date which is stored in a vector of size four to the days of the week using the weekday’s function in r language.

R




date <- data.frame(date = as.Date(c("2020-10-11","2000-10-01",
                                    "1999-12-08","2021-05-05")))
weekday <- weekdays(date$date)                
print(weekday)

Output:

[1] "Sunday"    "Sunday"    "Wednesday" "Wednesday"

Method 2: Using strftime() function.

In this method of converting the date of the week, the user needs to call strftime() function which is an in-built function and pass the respected parameters into it, and then in return, the function will give the weekdays of the given date to the user.



strftime function: This function is used to convert between character representations and objects of classes “POSIXlt” and “POSIXct” representing calendar dates and times.

Syntax: strptime(x, format, tz = “”)

Parameters:

  • x: An object to be converted: a character vector for strptime, an object which can be converted to “POSIXlt” for strftime.
  • tz: A character string specifying the time zone to be used for the conversion.
  • format: A character string. The default for the format methods is “%Y-%m-%d %H:%M:%S” if any element has a time component that is not midnight, and “%Y-%m-%d” otherwise.

Example:

In this example, we will be converting the given date which is stored in a vector of size four to the days of the week using the strftime function in R language.

R




date <- data.frame(date = as.Date(c("2020-10-11","2000-10-01",
                                    "1999-12-08","2021-05-05")))
weekday <- strftime(date$date, "%A")
print(weekday)

Output:

[1] "Sunday"    "Sunday"    "Wednesday" "Wednesday"

Method 3: Using as.POSIXlt() function

In this method of converting date to day of the week, users need to call as.POSIXlt() function, which is an in-built function, here the user has to pass the required parameters into this function and with this in return, the user will be getting the weekdays of all the date given.

as.POSIXlt() function: This function is used to manipulate objects of classes “POSIXlt” and “POSIXct” representing calendar dates and times.

Syntax: as.POSIXlt(x, tz = “”, …)

Parameters:

  • x:-R object to be converted.
  • tz:-time zone specification to be used for the conversion if one is required. System-specific, but “” is the current time zone, and “GMT” is UTC
  • …:-further arguments to be passed to or from other methods.

In this example, we will be converting the given date which is stored in a vector of size four to the days of the week using the as.POSIXlt() function in R language.

R




date <- data.frame(date = as.Date(c("2002-12-11",   
                                        "2000-07-11",
                                        "1900-12-11",
                                        "2015-11-11")))
  
weekday <-c("Monday", "Tuesday", "Wednesday",
                "Thursday", "Friday", "Saturday",
                "Sunday")[as.POSIXlt(date$date)$wday]
print(weekday)

Output:

[1] "Wednesday" "Tuesday"   "Tuesday"   "Wednesday"



My Personal Notes arrow_drop_up
Recommended Articles
Page :