I was reviewing some code and came across this odd result. If you have a dataframe with one value of type integer and you coerce it to integer you get what I think you would expect:
library(dplyr)
tibble(x = as.integer(c(1))) %>% as.integer()
[1] 1
But if it's of type int64, you get something weird:
library(bit64)
tibble(x = as.integer64(c(1))) %>% as.integer()
[1] 0
What gives? I assume it has something to do with the int64
class. But why would I get zero? Is this just bad error handling?
Update
OK, there's a hint to what's going on when you call dput
on the int64
dataframe:
structure(list(x = structure(4.94065645841247e-324,
class = "integer64")),
row.names = c(NA, -1L),
class = c("tbl_df", "tbl", "data.frame"))
So as.integer()
is rightly converting 4.94065645841247e-324 to zero. But why is that what's stored in the DF?
Also, to see that this is not a bit64
issue, I get a very similar structure on the actual df I get back from my database:
structure(list(max = structure(2.78554211125295e-320,
class = "integer64")),
class = "data.frame",
row.names = c(NA, -1L))
double
is how the integer is being represented in the R object storing the data, like I mentioned below. The class just help informs r which method to use when you want to convert. In fact the DBI documentation mentionsbit64
specifically – Pedagogicsint64
an option in base r? – Penult