user1471980 user1471980 - 1 month ago 14
R Question

unable to convert to date time in R

structure(list(Datetime = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "2016-10-19 00:00:00", class = "factor")), .Names = "Datetime", row.names = c(NA,
-6L), class = "data.frame")


when I try to convert Datetime to POSIXct as this:

t$Datetime<-as.POSIXct(t$Datetime, format="%Y-%m-%d %H:%M:%S")


I lose hour, minute and second from the data. It becomes like this:

Datetime
1 2016-10-19
2 2016-10-19
3 2016-10-19
4 2016-10-19
5 2016-10-19
6 2016-10-19

dput(t)
structure(list(Datetime = structure(c(1476849600, 1476849600,
1476849600, 1476849600, 1476849600, 1476849600), class = c("POSIXct",
"POSIXt"), tzone = "")), .Names = "Datetime", row.names = c(NA,
-6L), class = "data.frame")


why I am losing the hour, minute seconds from the data?

Answer

The new library anytime is an alternative, easy-to-use library. Do this:

to install:
install.packages("anytime")

to convert your values:
library(anytime)
anydate(t$Datetime)