max
max

Reputation: 4521

lubridate::as_datetime() fails when as.POSIXct() works?

Could someone explain why lubridate::as_datetime() fails here, but as.POSIXct() works?

> as.POSIXct("2020-10-27 20:25")
[1] "2020-10-27 20:25:00 CDT"

> lubridate::as_datetime("2020-10-27 20:25")
[1] NA
Warning message:
All formats failed to parse. No formats found. 

Upvotes: 0

Views: 244

Answers (1)

Jagge
Jagge

Reputation: 968

Why the one works and not the other I don't know, but you can help as_datetime() understand the input by suppling a format string, which specifies the format the text string.

lubridate::as_datetime("2020-10-27 20:25", format = "%Y-%m-%d %H:%M")

Check out the documentation for as_datetime() and strptime() on how to write the format-string.

edit: It seems that the format argument defaults to NULL for as_datetime, a similar error is generated by as.Posixct() if format = NULL is supplied.

as.POSIXct("2020-10-27 20:25", format = NULL)

Upvotes: 1

Related Questions