这是我的示例数据,我不明白为什么列在使用包ITime
的以下代码中丢失了它的格式data.table
DT = data.table(x=as.POSIXct(c("2009-02-17 17:29:23.042",
"2009-02-17 17:29:25.160")),
y=c(1L,2L))
DT[,x1:=as.ITime(x)]
DT[,`:=`(last.x=tail(x,1L),last.x1=tail(x1,1L)),by=y]
DT
x y x1 last.x last.x1
1: 2009-02-17 17:29:23.042 1 17:29:23 1234888163 62963
2: 2009-02-17 17:29:25.160 2 17:29:25 1234888165 62965
但是如果data.table
已经知道下面的格式,它可以工作
DT = data.table(x=as.POSIXct(c("2009-02-17 17:29:23.042",
"2009-02-17 17:29:25.160")),
y=c(1L,2L))
DT[,x1:=as.ITime(x)]
DT[,`:=`(last.x=x,last.x1=x1)] #HERE DATA>TABLE KNOWS THE LAST.* FORMAT
DT[,`:=`(last.x=tail(x,1L),last.x1=tail(x1,1L)),by=y]
R) DT
x y x1 last.x last.x1
1: 2009-02-17 17:29:23.042 1 17:29:23 2009-02-17 17:29:23.042 17:29:23
2: 2009-02-17 17:29:25.160 2 17:29:25 2009-02-17 17:29:25.160 17:29:25
它必须是关于如何data.table
分配的东西,有解决方法吗?
更新感谢 Arun,现在已修复
R) library(data.table)
data.table 1.8.11 For help type: help("data.table")
R) DT = data.table(x=as.POSIXct(c("2009-02-17 17:29:23.042",
+ "2009-02-17 17:29:25.160")),
+ y=c(1L,2L))
R) DT[,x1:=as.ITime(x)]
R) DT[,`:=`(last.x=tail(x,1L),last.x1=tail(x1,1L)),by=y]
R) DT
x y x1 last.x last.x1
1: 2009-02-17 17:29:23.042 1 17:29:23 2009-02-17 17:29:23.042 17:29:23
2: 2009-02-17 17:29:25.160 2 17:29:25 2009-02-17 17:29:25.160 17:29:25