0
  1. I have a data frame (1 x 30 in dimensions) made entirely of numeric columns (double and integer columns). Some of the integers are 10^12.
  2. When I convert the data frame to a matrix using as.matrix() function of base R, something strange happens, an integer like, for example here, 10978645435 would not stay the same in the new object (numeric matrix) but it becomes 5.076377571e-314

Any idea why would this happen and how I could possibly fix the issue? Thanks in advance.

  • 2
    Can you post sample data in `dput` format? Please edit **the question** with the output of `dput(df)`. Or, if it is too big with the output of `dput(head(df, 20))`. – Rui Barradas Dec 27 '18 at 15:50
  • Thanks Rui. The data is read from a file and I cannot recreate it using simple code. Since I am using bit64 package, some integers are read as integer64 and these are the integers that get deformed. It looks like the matrix does not allow integer64 data type. Or it has to do with the object size limit in the memory or something. –  Dec 27 '18 at 16:16
  • reproducible example: `library(bit64); dd <- data.frame(x=as.integer64("10978645435")); as.matrix(dd)` – Ben Bolker Dec 27 '18 at 16:21
  • Thanks Ben. You got it. I was not aware of how to specify integer64 for a data entry. Thanks again. –  Dec 27 '18 at 16:24

1 Answers1

0

Hi maybe you should take a look to Why are values changing when converting from data.frame to a numeric matrix?

Look at Alex A.'s answer and tell me if it is helping you. I also think it is because the numeric values in your data frame are being treated as factors.

Alex A.'s code : y <- apply(as.matrix(x[, 1:5]), 2, as.numeric)

Edit : Nevermind seems like you have found your problem.

Gainz
  • 1,721
  • 9
  • 24
  • Thanks Gainz. All data in the data frame is treated as int, integer64, or numeric. –  Dec 27 '18 at 16:18
  • @JosephAmram Seems related to your question? https://stackoverflow.com/questions/28262301/how-to-convert-a-data-frame-of-integer64-values-to-be-a-matrix – Gainz Dec 27 '18 at 16:19