这是我的示例数据,我不明白为什么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
答案 0 :(得分:2)
更新:这已在v1.8.11中修复。来自NEWS:
:=
(通过引用分配)丢失POSIXct
或ITime
属性,而分组现已修复,#2531。测试补充说。感谢stat stat for reporting: Why does this POSIXct or ITime loses its format/attribute和Paul Murray在此报道SO: Cannot assign columns as.Date by reference in data.table
如果我似乎忽略了某些事情,请回信。
答案 1 :(得分:1)
归档为bug #2531,马修更新优先级。