How to convert dates into days of week in R?

r

#1

I was going through a dataset, where I had date and time in a single variable in the following format:

2011-01-01 01:00:00

I want to convert this date into the corresponding day of the week.Could you suggest some function for the same?

Thanks in advance!


#2

@shashwat.2014
Hi there,

You can make use of the “Lubridate” package in R to parse the date and then calculate the day of the week.

For eg:
library(lubridate)
date<-ymd_hms(“2016-06-06 09:45:12”)
wday(date)

**[2]**

Here. 1 means Sunday…and so on.

Hope it helps.`


#3

Ensure that your date field is in proper format. (POSIXt)

You can use the function weekdays from base R for this.
Example:

weekdays(as.POSIXct(“2016-06-06 10:00:12”), abbreviate = F)
[1] "Monday"
weekdays(as.POSIXct(“2016-06-06 10:00:12”), abbreviate = T)
[1] “Mon”


#4

Thanks a lot!