为什么这个POSIXct或ITime失去其格式/属性 [英] Why does this POSIXct or ITime loses its format/attribute
问题描述
这里是我的示例数据,我不明白为什么 ITime
列失去它的格式在以下代码使用 data.table
package
here is my sample data, I do not understand why the ITime
column loses it's format in the following code using the data.table
package
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
已经知道格式如下,它工作
But If data.table
already knows the format like in the following, It works
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
分配的,是否有工作?
It must be something about how data.table
assigns, is there a work around ?
UPDATE 感谢Arun这是现在固定的
UPDATE Thanks to Arun this is now fixed
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
推荐答案
更新:这已在v1.8.11中修复。从新闻:
Update: This has been fixed in v1.8.11. From NEWS:
:=
失去POSIXct
或ITime
属性现在固定,#2531 。测试添加。感谢报告的统计量:
为什么这个POSIXct或ITime失去它的格式/属性和保罗·穆雷在这里报告SO:
无法在data.table中将列分配为.Date by reference。
:=
(assignment by reference) losesPOSIXct
orITime
attribute while grouping is now fixed, #2531. Tests added. Thanks to stat quant for reporting here: Why does this POSIXct or ITime loses its format/attribute and to Paul Murray for reporting here on SO: Cannot assign columns as.Date by reference in data.table
如果我似乎忽略了某些东西,请回信。
Please write back if I seem to have overlooked something.
这篇关于为什么这个POSIXct或ITime失去其格式/属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!