Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this POSIXct or ITime loses its format/attribute

Tags:

r

data.table

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
like image 471
statquant Avatar asked Jan 30 '13 13:01

statquant


2 Answers

Update: This has been fixed in v1.8.11. From NEWS:

:= (assignment by reference) loses POSIXct or ITime 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.

like image 138
Arun Avatar answered Sep 23 '22 13:09

Arun


Filed as bug #2531, priority updated by Matthew.

like image 26
statquant Avatar answered Sep 23 '22 13:09

statquant