0

I was attempting to add a new column to a data table which would be of class POSIXct, but I found that the data keeps getting converted to numeric for some reason ... but only when I use 'by'. Is this a bug, or a limitation, or am I doing something wrong?

dt <- data.table(x = c(rep(1,2), rep(2,2)), y=1:4)
dt[, z:=as.POSIXct("2014-01-01")]
>dt

   x y          z
1: 1 1 2014-01-01
2: 1 2 2014-01-01
3: 2 3 2014-01-01
4: 2 4 2014-01-01

So far so good...

dt <- data.table(x = c(rep(1,2), rep(2,2)), y=1:4)
dt[, z:=as.POSIXct("2014-01-01"), by=x]
>dt

   x y          z
1: 1 1 1388534400
2: 1 2 1388534400
3: 2 3 1388534400
4: 2 4 1388534400

Not what I was expecting!

Hmm.... I wonder... does data table grouping rely on lapply? If so, it doesn't support POSIXct, because lapply does type conversions -- is that right?

Arun
  • 116,683
  • 26
  • 284
  • 387
pteehan
  • 807
  • 9
  • 19

0 Answers0