Search code examples
rclassdataframereshape2

Integer64 class doesn't survive reshape2 melt function


I don't know whether this is an integer64 (from bit64) problem, or a melt problem (from reshape2, but if I try to reshape a data.frame containing integer64 data then the class information is destroyed in the process and it reverts to the double representation:

library(bit64)
library(reshape2)

DF = data.frame(I =letters, Num1 = as.integer64(1:26), Num2 = as.integer64(1:26))
DFM = melt(DF, id.vars = "I")

sapply(DF, class)
sapply(DFM, class)

gives:

> sapply(DF, class)
          I        Num1        Num2 
   "factor" "integer64" "integer64" 
> sapply(DFM, class)
        I  variable     value 
 "factor"  "factor" "numeric" 

And because integer64 is double underneath, the data is "corrupted"

> DF
   I Num1 Num2
1  a    1    1
2  b    2    2
3  c    3    3
4  d    4    4
5  e    5    5
...
> DFM
   I variable         value
1  a     Num1 4.940656e-324
2  b     Num1 9.881313e-324
3  c     Num1 1.482197e-323
4  d     Num1 1.976263e-323
5  e     Num1 2.470328e-323
6  f     Num1 2.964394e-323

What is causing this? Is this a integer64 problem or a melt problem? When creating classes what can be done to avoid this sort of thing?


Solution

  • It seems to be a limitation of the package which is also mentioned in their documentation here on page 9. For example:

    x <- data.frame(a=as.integer64(1:5), b=as.integer64(1:5))
    > x
    #   a b
    # 1 1 1
    # 2 2 2
    # 3 3 3
    # 4 4 4
    # 5 5 5
    
    > unlist(x)
    
    #            a1            a2            a3            a4            a5            b1 
    # 4.940656e-324 9.881313e-324 1.482197e-323 1.976263e-323 2.470328e-323 4.940656e-324 
    #            b2            b3            b4            b5 
    # 9.881313e-324 1.482197e-323 1.976263e-323 2.470328e-323 
    
    > as.matrix(x)
    #                  a             b
    # [1,] 4.940656e-324 4.940656e-324
    # [2,] 9.881313e-324 9.881313e-324
    # [3,] 1.482197e-323 1.482197e-323
    # [4,] 1.976263e-323 1.976263e-323
    # [5,] 2.470328e-323 2.470328e-323
    
    x <- as.integer64(1:5)
    
    > is.vector(x)
    # [1] FALSE
    
    > as.vector(x)
    # [1] 4.940656e-324 9.881313e-324 1.482197e-323 1.976263e-323 2.470328e-323