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.
We need the format
argument and it can be converted using as.POSIXct
alone without resorting to strptime/as.POSIXct
my_table[, my_dates := as.POSIXct(my_dates, format = "%m/%d/%Y %H:%M")]
my_table
# my_dates this that
#1: 2016-12-01 15:30:00 a 1
#2: 2016-12-01 15:31:00 b 2
#3: 2016-12-01 15:32:00 c 3
The reason for the warning is because of the order of arguments in as.POSIXct/strptime
. If we check ?as.POSIXct
, the usage is
as.POSIXct(x, tz = "", ...)
That means, without specifying the format
, it thinks that the second argument is for tz
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With