I have a data frame consisting entirely of integer64
columns that I'd like to convert to be a matrix.
library(bit64)
(dfr <- data.frame(x = as.integer64(10^(9:18))))
## x
## 1 1000000000
## 2 10000000000
## 3 100000000000
## 4 1000000000000
## 5 10000000000000
## 6 100000000000000
## 7 1000000000000000
## 8 10000000000000000
## 9 100000000000000000
## 10 1000000000000000000
Unfortunately, as.matrix
doesn't give the correct answer.
(m <- as.matrix(dfr))
## x
## [1,] 4.940656e-315
## [2,] 4.940656e-314
## [3,] 4.940656e-313
## [4,] 4.940656e-312
## [5,] 4.940656e-311
## [6,] 4.940656e-310
## [7,] 4.940656e-309
## [8,] 5.431165e-308
## [9,] 5.620396e-302
## [10,] 7.832953e-242
The problem seems to be that integer64
values are stored as numeric values with an "integer64" class attribute (plus some magic to make them print and do arithmetic correctly) that gets stripped by as.matrix
.
I can't just do class(m) <- "integer64"
because that changes the class of the matrix object not its contents.
Likewise, mode(m) <- "integer64"
gives the wrong answer and typeof(m) <- "integer64"
and storage.mode(m) <- "integer64"
throw errors.
Of course I could just circumvent the problem by converting the columns to double (dfr$x <- as.double(dfr$x)
) but it feels like there ought to be a way to do this properly.
How can I get a matrix of integer64
values?
integer64
methods, you'll still end up convering to doubles at some point. Does thebit64
package have a collection of appropriate methods, and for that matter does it have someas.matrix64
tool? If not you might as well convert to double or perhaps usegmp
andRmpfr
packages. ETA: the word "matrix" doesn't even appear in the Help file forbit64
, so I think you'll have to stick with dataframes and the available methods for that. – Trichroism