My data.table has a column with an "ambiguous" datetime format: "12/1/2016 15:30". How can I convert this datetime to a format R recognizes in a data.table without using strptime()
and getting the warning message for initially converting to POSIXlt. The process works but the warning makes me think there is another way.
My data table:
my_dates <- c("12/1/2016 15:30", "12/1/2016 15:31", "12/1/2016 15:32")
this <- c("a", "b", "c")
that <- c(1, 2, 3)
my_table <- data.table(my_dates, this, that)
my_table
my_dates this that
1: 12/1/2016 15:30 1 a
2: 12/1/2016 15:31 2 b
3: 12/1/2016 15:32 3 c
my_table[, my_dates := as.POSIXct(strptime(my_dates, "%m/%d/%Y %H:%M"))]
Warning message:
In strptime(my_dates, "%m/%d/%Y %H:%M") :
POSIXlt column type detected and converted to POSIXct. We do not
recommend use of POSIXlt at all because it uses 40 bytes to store one date.
So, it works but I bet there is a technique that I have simply overlooked to avoid this warning. I used my_table[, dates:= as.POSIXct(dates)]
but this drops the time portion of the datetime. I have also tried my_table[, dates:= as.POSIXct(dates, "%m/%d/%Y %H:%M")]
and the time is dropped as well and warnings are retruned.
Thank you for advice.