Search code examples
rdata.tabledcast

Unexpected behavior of dcast with integer64 numbers


I am working with international trade data from Comtrade, which was given to me as csv file. The trade values are stored as integer64. I am aware that I should read a lot more about what can and cant be done when using such a format.

A simple example:

library("data.table")
library("bit64")
d <- data.table("ID" = c("a","a","b","b"),
            "type" = c("v1","v2","v1","v2"),
            "value"= (sample(1:100,4) %>% as.integer64) )

dcast(d[,], ID ~ type, fill = NA)
    ID v1 v2
1:  a 65  7
2:  b 63 91

dcast(d[-2,], ID ~ type, fill = NA)
   ID v1                  v2
1:  a 65 9218868437227407266
2:  b 63                  91

Where does the "9218868437227407266" come from? I would expect to see an NA there.


Solution

  • with base 64 you need to use NA_integer64_ instead of NA

    dcast(d[-2,], ID ~ type, fill = NA_integer64_)
    
    
       ID v1   v2
    1:  a 67 <NA>
    2:  b 79   76