Reputation: 2663
Super easy question, that somehow I can't figure out by reading the documentation.I am reading in a date/time variable into POSIXlt form as follows:
data$date <-strptime(unformatted.date, %m/%d/%Y %H:%M)
Then, I am trying to create a factor variable representing the weekday:
data$weekday <- as.POSIXlt(data$date, format="%A")
This returns a variable that is NA. Help! (And I apologize if this is something most people can get from the documentation...I really have read around, and can't find the answer).
Upvotes: 1
Views: 6055
Reputation: 951
library(lubridate)
ttt<-strptime("01/12/2018 18:00", "%m/%d/%Y %H:%M")
wday(ttt)
[1] 6 #Sunday=1
wday(ttt,label=TRUE)
[1] Fri
This approach can save you conversion if you need to work with numbers
Upvotes: 0
Reputation: 132969
ttt<-strptime("07/20/2012 18:00", "%m/%d/%Y %H:%M")
ttt
weekdays(ttt)
#[1] "Friday"
This can be found out by reading ?POSIXlt
carefully.
PS: factor(ttt$hour)
Upvotes: 4