Search code examples
rdatestrptime

How to get the appropriate weekdays to show up?


I've inputted the following code:

exdata1$DateTime <- strptime(paste(exdata1$Date, exdata1$Time), format="%d/%m/%Y %H:%M:%S") 

library(lubridate)
summary(wday(exdata1$DateTime, label=TRUE))

and it gives me

 Sun  Mon  Tue  Wed  Thu  Fri  Sat NA's 
   0    0    0    0    0    0    0 2880 

But it should be giving me 1440 for both Thursday and Friday.

How to improve?


Solution

  • You have the wrong format. In base R you can try :

    exdata1$DateTime <- strptime(paste(exdata1$Date, exdata1$Time), 
                                 format="%Y-%m-%d %H:%M:%S") 
    

    Can also use as.POSIXct instead of strptime and then use weekdays function

    weekdays(exdata1$DateTime)
    

    With lubridate, you can do

    library(lubridate)
    
    exdata1$DateTime <- ymd_hms(paste(exdata1$Date, exdata1$Time))
    wday(exdata1$DateTime, label = TRUE)