0

Dealing with data saved as integer64 make me wondering if there is a better way. Altough there are discussion in many thread including this one Convert integer64 into integer in R. I still have hard time to find the correct answer.

It's simple question but it becomes annoying for me to handle. For example, I have multiple SQL queries, which come back as a single number of counts. I wanted to take a look at all the numbers but the integer64 formats make it confusing.

I tried to follow the suggestion of converting to double or integer but all failed. Is there a way to do this correctly? I tried following but failed. Any tips?

Not reproducible examples but I provided some examples here.

# suppose I queried multiple time as this one. 
usum <- tbl(con,'#other_glucose_pool') %>% tally() %>% as_tibble()

# supposes the count numbers as following:
# usum : 68899
# csum : 732878865
# tsum : 1953359937
# ggg : 1151515151515165

# usum$n
integer64
[1] 68899

# I was trying to get only those ending with sum
needs <- ls()[grep('sum$',ls())]
lapply(needs,function(x) as.double(get(x)))

# result that the scientific notation is still there.

As suggested, I included an example below.

dput(unchgsum)
structure(list(n = structure(5.9575917772475e-319, class = "integer64")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -1L))

nnn <- as.numeric(unchgsum)
nni <- as.integer(unchgsum)
nnd <- as.double(unchgsum)
nnc <- as.character(unchgsum)

# nnn
# [1] 5.957592e-319
# nni
# [1] 0
# nnd
# [1] 5.957592e-319
# nnn
# [1] 5.957592e-319
# nnc
[1] "5.9575917772475e-319"

ponyhd
  • 491
  • 1
  • 4
  • 19
  • In what way are you having trouble with the `integer64` values? Integer64 values exists because normal R integers can't hold values that large. You can't coerce to numeric (double or "regular" integer) without a loss of precision. If you need numbers that large, they must be stored inside of a special integer64 data type. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick May 25 '23 at 18:06
  • Thanks for quick response. I added one reproducible example. – ponyhd May 25 '23 at 18:20
  • I'm trying to convert integer64 number to human readable format either integer, numeric or character. – ponyhd May 25 '23 at 18:30
  • What about `as.character(unchgsum$n)` with your example? Do you have the `bit64` library loaded? – MrFlick May 25 '23 at 18:34
  • I loaded bit64. Yes, as.character(unchgsum$n) will work, but I wanted to convert so many of them, using lapply(needs,function(x) as.character(get(x$n))) won't work. Any suggestion? – ponyhd May 25 '23 at 18:51
  • I still don't understand what you are trying to do. Maybe you meant `lapply(needs,function(x) as.character(get(x)$n))`? – MrFlick May 25 '23 at 18:53
  • Yes, you are right, that's what I tried to solve. ```lapply(needs,function(x) as.character(get(x)$n)) ``` I got Error in get(x) : invalid first argument – ponyhd May 25 '23 at 18:55
  • What's contained in `needs`? I can't replicate your error. If I create `unchgsum` as in your example, then set `needs <- "unchgsum"`, `lapply(needs,function(x) as.character(get(x)$n))` will run for me without error. – MrFlick May 25 '23 at 19:06
  • Not sure what your desired output is, but the `[scales](https://scales.r-lib.org/)` package has a lot of helpful functions for formatting number is a human readable way. They are made to be used for axis labels, but they work for any text output. I would try `scales::label_scientific(digits = 5)(df$col)` – mfg3z0 May 25 '23 at 19:25
  • @MrFlick Looking deeper I found out that the error I came across was due to the list has mixed type of class. Your suggestion works if the same type of class in the list. Please answer this question so that I can accept your suggestion as the answer. Thanks at lot. – ponyhd May 25 '23 at 21:23

0 Answers0