user1471980 user1471980 - 2 months ago 13
R Question

how do you convert a factor that has 3 digits miliseconds to datetime in R?

I time a t object like this:

t<-c("2016-10-19 00:00:00.000")

I need to convert to POSIXct as this:

as.POSIXct(t, format="%Y-%m-%d %H:%M:%OS")

"2016-10-19 EDT"

I loose the hour, minute and second. Any ideas what I am doing wrong here?

I need this to be

2016-10-19 00:00:00


You didn't lose them, that's just the default for when the hours, minutes and seconds are zero. You can specify a format string to force these to be printed:

> ct <- as.POSIXct(t, format="%Y-%m-%d %H:%M:%OS")
> format(ct, "%Y-%m-%d %H:%M:%S")
[1] "2016-10-19 00:00:00"