Question

Cannot figure out what I am doing wrong here.

require(data.table)
dt = data.table( ts=seq( ISOdate(2014,1,1), ISOdate(2014,1,2), by='hour') )
dt[, day:=trunc(ts, 'days') ]

Gives me a list of length 9 in column "day"?!

v = trunc( dt$ts, 'days' )

Gives me a vector of length 25.

dt[, day.test:=v ]

Gives me again a list of length 9 in column "day.test"?!

Was it helpful?

Solution

Try this:

dt[, day:=as.POSIXct(trunc(ts, 'days')) ]

From ?data.table

   POSIXlt is not supported as a column type because it uses 40 bytes to
store a single datetime. Unexpected errors may occur if you manage to create
a column of type POSIXlt. Please see NEWS for 1.6.3, and IDateTime instead.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top