PtitBleu
2011-Feb-09 12:15 UTC
[R] Problem with long number (from character to numeric class)
Hello, I have a text file with one column containing long number but stored as string. I download the file with read.table (and colClass) and the first row of this column is : "095842087016731010" As I need to make some calculations with these numbers, I tried to convert them using as.numeric. But then I get as.numeric("095842087016731010") 95842087016731008 I understand the disappearing of the first 0 but I don't understand the substitution of the last 0 by 8. Is there a way to avoid that ? Thanks in advance, Ptit Bleu. -- View this message in context: http://r.789695.n4.nabble.com/Problem-with-long-number-from-character-to-numeric-class-tp3297121p3297121.html Sent from the R help mailing list archive at Nabble.com.
Ben Bolker
2011-Feb-09 14:05 UTC
[R] Problem with long number (from character to numeric class)
PtitBleu <ptit_bleu <at> yahoo.fr> writes:> I have a text file with one column containing long number but stored as > string. > I download the file with read.table (and colClass) and the first row of this > column is : > > "095842087016731010" > > As I need to make some calculations with these numbers, I tried to convert > them using as.numeric. > But then I get > as.numeric("095842087016731010") > 95842087016731008 >I think you are running into the limitations of double precision values. Take a look at the inverse of your value and compare it to the "eps" values in .Machine. Then see the R wiki (search for "precision") for alternatives for extended precision calculations. Ben Bolker
Marc Schwartz
2011-Feb-09 14:12 UTC
[R] Problem with long number (from character to numeric class)
On Feb 9, 2011, at 6:15 AM, PtitBleu wrote:> > Hello, > > I have a text file with one column containing long number but stored as > string. > I download the file with read.table (and colClass) and the first row of this > column is : > > "095842087016731010" > > As I need to make some calculations with these numbers, I tried to convert > them using as.numeric. > But then I get > as.numeric("095842087016731010") > 95842087016731008 > > I understand the disappearing of the first 0 but I don't understand the > substitution of the last 0 by 8. > > Is there a way to avoid that ? > Thanks in advance, > Ptit Bleu.I get:> as.numeric("095842087016731010")[1] 9.584209e+16 # See ?as.character> as.character(as.numeric("095842087016731010"))[1] "95842087016731008" I suspect you would need to look at using one of the R packages that support arbitrary/infinite precision representations if you need to deal with very large integers. Perhaps look at gmp and Rmpfr. You are hitting R's limit of 15 significant digits, resulting in the loss of precision in the internal representation of the number, which is in turn, presented in the resultant coercion. HTH, Marc Schwartz
jim holtman
2011-Feb-09 14:15 UTC
[R] Problem with long number (from character to numeric class)
FAQ 7.31 For 'numerics' there is a limit of about 15 digits of accuracy in the number. On Wed, Feb 9, 2011 at 7:15 AM, PtitBleu <ptit_bleu at yahoo.fr> wrote:> > Hello, > > I have a text file with one column containing long number but stored as > string. > I download the file with read.table (and colClass) and the first row of this > column is : > > "095842087016731010" > > As I need to make some calculations with these numbers, I tried to convert > them using as.numeric. > But then I get > as.numeric("095842087016731010") > 95842087016731008 > > I understand the disappearing of the first 0 but I don't understand the > substitution of the last 0 by 8. > > Is there a way to avoid that ? > Thanks in advance, > Ptit Bleu. > -- > View this message in context: http://r.789695.n4.nabble.com/Problem-with-long-number-from-character-to-numeric-class-tp3297121p3297121.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
David Winsemius
2011-Feb-09 14:28 UTC
[R] Problem with long number (from character to numeric class)
On Feb 9, 2011, at 7:15 AM, PtitBleu wrote:> > Hello, > > I have a text file with one column containing long number but stored > as > string. > I download the file with read.table (and colClass) and the first row > of this > column is : > > "095842087016731010" > > As I need to make some calculations with these numbers, I tried to > convert > them using as.numeric. > But then I get > as.numeric("095842087016731010") > 95842087016731008Your number is many powers of ten above the maximum possible integer value 2*10^9. So you are seeing the floating point number that resulted from coercion, and R does not have arbitrary precision arithmetic in its base repertoire.> I understand the disappearing of the first 0 but I don't understand > the > substitution of the last 0 by 8. > > Is there a way to avoid that ?There is a package that handles bignums, Brobdingnag. . And there is interfaces to the symbolic algebra system, Yacas. But.... Do you really need arbitrary precision?> Thanks in advance, > Ptit Bleu. > --David Winsemius, MD West Hartford, CT
Daniel Nordlund
2011-Feb-09 17:00 UTC
[R] Problem with long number (from character to numeric class)
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > On Behalf Of PtitBleu > Sent: Wednesday, February 09, 2011 4:15 AM > To: r-help at r-project.org > Subject: [R] Problem with long number (from character to numeric class) > > > Hello, > > I have a text file with one column containing long number but stored as > string. > I download the file with read.table (and colClass) and the first row of > this > column is : > > "095842087016731010" > > As I need to make some calculations with these numbers, I tried to convert > them using as.numeric. > But then I get > as.numeric("095842087016731010") > 95842087016731008 > > I understand the disappearing of the first 0 but I don't understand the > substitution of the last 0 by 8. > > Is there a way to avoid that ? > Thanks in advance,What kind of processing do you need to do with these numbers? There may be better ways to handle the problem. Dan Daniel Nordlund Bothell, WA USA