I am trying to load a dataset into r and change the type of a column into datetime.
strptime
, as.POSIXct
or as.Date
for different cases should work;
This is my code:
a <- structure(list(DATE = c("01/01/2011 12:00:00", "01/02/2011 12:00:00",
"01/03/2011 12:00:00", "01/04/2011 12:00:00", "01/05/2011 12:00:00",
"01/06/2011 12:00:00"), VAL = c(65.34447917, 65.23983333, 65.03183333,
64.89107292, 64.83333333, 64.848625), id = c("VT1-1", "VT1-1", "VT1-1",
"VT1-1", "VT1-1", "VT1-1")), .Names = c("DATE", "VAL", "id"), row.names = c(NA, -6L),
class = c("tbl_df", "tbl", "data.frame"))
b1 <- as.POSIXct(a$DATE, format = "%m/%d/%y %H:%M:%S")
b2 <- strptime(a$DATE,"%m/%d/%Y %H:%M/%S")
But they just return NA
. It is most probably a typo; but how this can be avoided while dealing with different date-time formats?