1

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).

Artem Klevtsov
  • 9,193
  • 6
  • 52
  • 57
roody
  • 2,633
  • 5
  • 38
  • 50

2 Answers2

4
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)

Roland
  • 127,288
  • 10
  • 191
  • 288
0
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

3pitt
  • 899
  • 13
  • 21