cryptickey
cryptickey

Reputation: 308

Iterating through an integer 64 array prints incorrect values in R

Do for loop and lapply not inherently support integer64 ?

> x <- as.integer64(c("100000000000", "10000000000000"))
> x
integer64
[1] 100000000000   10000000000000
> for(y in x) {print(y)}
[1] 4.940656e-313
[1] 4.940656e-311
> tmp <- lapply(x, function(y) {print(y)})
[1] 4.940656e-313
[1] 4.940656e-311
> for(i in 1:length(x)) {print(x[i])}
integer64
[1] 100000000000
integer64
[1] 10000000000000
> as.list(x)
[[1]]
[1] 4.940656e-313

[[2]]
[1] 4.940656e-311
> as.list(as.integer64(x[1]))
[[1]]
[1] 4.940656e-313


Upvotes: 1

Views: 132

Answers (1)

GKi
GKi

Reputation: 39707

You can store it in a list and iterate over the list:

library(bit64)
z <- list(as.integer64("100000000000"), as.integer64("10000000000000"))

for(y in z) {print((y))}
#integer64
#[1] 100000000000
#integer64
#[1] 10000000000000

tmp <- lapply(z, function(y) {print(y)})
#integer64
#[1] 100000000000
#integer64
#[1] 10000000000000

or use gmp which works for lapply:

library(gmp)
x <- as.bigz(c("100000000000", "10000000000000"))

lapply(x, function(y) {print(y)})
#Big Integer ('bigz') :
#[1] 100000000000
#Big Integer ('bigz') :
#[1] 10000000000000

Upvotes: 1

Related Questions