1

I am reading a csv file with some really big numbers like 1327707999760, but R automatically converts it into 1.32771e+12. I've tried to assign it a double class but it didn't work because it's already a rounded value. I've checked other posts like Preserving large numbers . People said "It's not in a "1.67E+12 format", it just won't print entirely using the defaults. R is reading it in just fine and the whole number is there." But when I tried to do some arithmetic things on them, it's just not right. For example:

test[1,8]
[1] 1.32681e+12
test[2,8]
[1] 1.32681e+12
test[2,8]-test[1,8]
[1] 0

But I know they are different numbers!

Community
  • 1
  • 1
Natalia
  • 369
  • 3
  • 15
  • They're not different if you get a difference of 0. – Matthew Lundberg Jun 23 '14 at 04:06
  • I'm guessing that `test[2,8] == test[1,8]` will return FALSE. – IRTFM Jun 23 '14 at 04:56
  • 1
    @BondedDust actually it returns TRUE. And I think I've found the problem. The thing is that I can't store the large numbers in my csv file! It always shows the scientific format so R can't read it precisely! Any solutions? – Natalia Jun 23 '14 at 05:16
  • Well then, you really ought to post a bit more detail, don't you agree? (And do look up colClasses in read.table, so that you can read these in as character.) – IRTFM Jun 23 '14 at 05:21
  • @BondedDust Sure. I mean, for example, when I generate the csv file from excel, 1329100082670 would appear like 1.329E+12. I can't make it show completely and I don't why. Then I found that R can't read 1.329E+12 in the csv precisely as 1329100082670. – Natalia Jun 23 '14 at 05:30
  • Well _that_ is just plain wrong. I had no trouble reading that value without loss of accuracy. `str(read.csv( text= c('1329100082670', '1329100082670') , header=FALSE)[1,1], digits=16)` returns `num 1329100082670` – IRTFM Jun 23 '14 at 05:33
  • @BondedDust oh seems the problem is not with R but the csv writer (Excel). I am still seeking some way to write the csv without converting into scientific format... – Natalia Jun 23 '14 at 05:49
  • So it's not really an R problem at all is it? Excel allows custom formats: Format/Cells/Custom and enter `#0`. – IRTFM Jun 23 '14 at 18:05

2 Answers2

5

That's not large. It is merely a representation problem. Try this:

options(digits=22)

options('digits') defaults to 7, which is why you are seeing what you do. All twelve digits are being read and stored, but not printed by default.

Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112
  • It seems I've found the real problem...that I can't store the large numbers in my csv file... It always shows the scientific format so R can't read it precisely! – Natalia Jun 23 '14 at 05:17
  • Well, it's modestly large. It is greater than the old limit on the size of an integer. – IRTFM Jun 23 '14 at 22:26
3

Excel allows custom formats: Format/Cells/Custom and enter #0

IRTFM
  • 258,963
  • 21
  • 364
  • 487