0

I have large numbers which are as character, I want to convert them to a number but this gives different output then expected:

> as.numeric("716897400001401732323")
[1] 716897400001401716736

What is the reason that R could not interpret the full number correctly, and how could I solve this problem?

Sorry it it not really a duplicate because I am using the melt function to melt an array with dimension names big numbers.

test <- array(c(1,2,3,4,5,6,7,8), dim = c(2,2,2), dimnames = list(c("122323232323232312121","23232323232323212121"),
                                                                  c("a","b"),
                                                                  c("c", "d")))

> melt(test)
                   Var1 Var2 Var3 value
1 122323232323232317440    a    c     1
2  23232323232323211264    a    c     2
3 122323232323232317440    b    c     3
4  23232323232323211264    b    c     4
5 122323232323232317440    a    d     5
6  23232323232323211264    a    d     6
7 122323232323232317440    b    d     7
8  23232323232323211264    b    d     8

To make it more clear: the problem could be solved in two ways, first of all melt should hold it as character but I did not receive an answer to this question see: Melt a array and make numeric values character So I thought I will make the column type as character and the problem is solved, but then the problem occurs that the conversion from character to numeric does not work because the numbers are to large.

Hopefully the question does make more sense now

Community
  • 1
  • 1
Tobias Dekker
  • 980
  • 8
  • 19
  • Because numbers this large are too big to store as integers, hence they are stored as double-precision floating point values with some loss of precision. This is very likely to be a duplicate. Can you give a little bit more context about what you need to do with these numbers? If you need to do arithmetic operations, you will need an extended-precision package such as `gmp`. – Ben Bolker Nov 15 '16 at 12:44
  • This is definitely a duplicate of the question that @Cath links to. I'm going to hold off for a little while on closing this; if you have some context that makes that answer inappropriate, go ahead and edit your question (re-opening is harder than closing). – Ben Bolker Nov 15 '16 at 12:47
  • Sorry, I edited my question, I don't think it is completely a duplicate – Tobias Dekker Nov 15 '16 at 12:49
  • 3
    But why do the `dimnames` need to be specified as numbers in the first place? Where do those numbers come from? If you used `dimnames = list(c("122323232323232312121","23232323232323212121"),...)` everything would be fine ... – Ben Bolker Nov 15 '16 at 12:53
  • the dimnames are automatically made character so it does not mather whether the dimnames input is numeric or character – Tobias Dekker Nov 15 '16 at 12:57
  • I'm not being clear. Why do they have enter the program as numbers in the first place? How are they generated/where do they come from? If they're typed directly into your code, just put quotation marks around them in the code and you're done. If they come from somewhere else, edit your question to tell us where they come from ... – Ben Bolker Nov 15 '16 at 13:02
  • 1
    so actually the problem si why this dimnames get converted into numeric inside the `melt` call – Cath Nov 15 '16 at 13:02
  • See the edits hopefully now it is more clear why I ask this question – Tobias Dekker Nov 15 '16 at 13:05
  • No, still working on it (commendable efforts though). The problem is **upstream** of the solutions you're trying. How did the rownames get to be numeric *in the first place*, in the actual context of the work you're doing? Where did they come from? – Ben Bolker Nov 15 '16 at 13:09
  • the problem comes from the line `lapply(dn[char], type.convert)` inside `melt.array` function : it converts you're character into numeric... so either ask Hadley to modify it into a parameter (so you can tell wether you want the conversion or not) or write your own `melt.array` function, without this line – Cath Nov 15 '16 at 13:10
  • The problem is solved in the dev version of `reshape`, you can use parameter `as.is`, set to `TRUE`, to avoid the conversion (so `melt(test, as.is=TRUE)`). (cf https://github.com/hadley/reshape/blob/master/README.md) – Cath Nov 16 '16 at 13:07

0 Answers0