Update: this seems to be a problem with data.table version 1.9.4, and not with the latest version of the package (1.9.6 at the time of this writing).
I have a table that I read through fread
as follows:
library(data.table)
library(bit64)
dt = fread('"x","y"\n2489751247,"a"\n2492940518,"b"\n2444706811,"a"\n2408767228,"b"')
:> x y
:> 1: 2489751247 a
:> 2: 2492940518 b
:> 3: 2444706811 a
:> 4: 2408767228 b
and I want the sum to x
be conditional on y
, but data.table gives the wrong answer:
dt[,.(total=sum(x)),by=y]
:> y total
:> 1: a 2.437946e-314
:> 2: b 2.421765e-314
without courtesy of a warning message. It turns out that x has an integer64 class:
lapply(dt,class)
:> $x
:> [1] "integer64"
:> $y
:> [1] "character"
so I can send s3 manually like this:
dt[,.(total=sum.integer64(x)),by=y]
:> y total
:> 1: a 4934458058
:> 2: b 4901707746
and for some reason, using class x in a sentence j
causes data.table to give the correct answer:
dt[,.(total=sum(x),cls=class(x)),by=y]
:> y total cls
:> 1: a 4934458058 integer64
:> 2: b 4901707746 integer64
which is strange. Is there any way to say to data.table
use S3 methods without using a class explicitly?