如何在不使用strptime的情况下转换data.table中的模糊日期时间列?

Dod*_*dge 5 datetime r data.table

我的data.table有一个"模糊"日期时间格式的列:"12/1/2016 15:30".如何将此日期时间转换为data.table中识别的格式R,而不使用strptime()并获取最初转换为POSIXlt的警告消息.这个过程有效,但警告让我觉得还有另一种方法.

我的数据表:

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.
Run Code Online (Sandbox Code Playgroud)

所以,它有效,但我敢打赌,有一种技术我只是忽略了以避免这种警告.我使用my_table[, dates:= as.POSIXct(dates)]但是这会丢弃日期时间的时间部分.我也尝试my_table[, dates:= as.POSIXct(dates, "%m/%d/%Y %H:%M")]了,时间也被删除,警告重新开始.

谢谢你的建议.

akr*_*run 7

我们需要format参数,它可以单独转换as.POSIXct而不需要求助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
Run Code Online (Sandbox Code Playgroud)

警告的原因是因为参数的顺序as.POSIXct/strptime.如果我们检查?as.POSIXct,用法是

as.POSIXct(x,tz ="",...)

这意味着,没有指定format,它认为第二个参数是为了tz