Why does as_datetime() result in NA in the below example?
x <- dmy("1-1-2000")
y <- "14:30"
as_datetime(paste(x, y))
Error:
[NA]
Warning message:
All formats failed to parse. No formats found.
Thank you
We can use the format
argument
library(lubridate)
as_datetime(paste(x, y), format = "%Y-%m-%d %H:%M")
#[1] "2000-01-01 14:30:00 UTC"
Or another option is anytime
library(anytime)
anytime(paste(x, y))
#[1] "2000-01-01 14:30:00 EST"
The reason could be that it expectss the time to be in %H:%M:%S
format, which the 'y' is not. If we use the full format instead of %H:%M
(which can also be judged as %M:%S
)
y1 <- "14:30:00"
as_datetime(paste(x, y1))
#[1] "2000-01-01 14:30:00 UTC"
NOTE: This answers why the OP get a warning message.
As you are using lubridate already, you just need to add the date object dmy
to the time object (hour : min) by setting hm
before.
x <- dmy("1-1-2000")
y <- "14:30"
z <- x + hm(y)
See : R tick data : merging date and time into a single object