简体   繁体   中英

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


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM