Method for sending S3 to data.table when using the `by` clause

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 freadas 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 xbe 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 jcauses 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.tableuse S3 methods without using a class explicitly?

+5
2

data.table 1.9.4, data.table(1.9.6 ). data.table :

installed.packages()['data.table','Version']

1.9.6, install.packages('data.table'). : R, Revolution Analytics, repos argment CRAN ( ) data.table 1.9.4:

install.packages('data.table',repos="http://my.favorite.CRAN.mirror/")

, . Rprofile.site, , :

if( packageVersion("data.table") == package_version('1.9.4'))
    install.packages("data.table",lib=Sys.getenv("R_LIBS_USER"),repos='http://my.favorite.CRAN.mirror')
+4
0

Source: https://habr.com/ru/post/1611986/


All Articles