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
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
It must be something about how data.table
assigns, is there a work around ?
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
Update: This has been fixed in v1.8.11. From NEWS:
:=
(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.
Filed as bug #2531, priority updated by Matthew.
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