0

I'm trying to do a simple sum over a large column in R. The answer comes back all right, but not to the specificity that I want. For example:

> tail(x)
              [,1]
[1999995,] 1999995
[1999996,]       0
[1999997,] 1999997
[1999998,]       0
[1999999,] 1999999
[2e+06,]         0

If I do a sum(x), I get:

> sum(x)
[1] 1e+12

Which is fine, but I'd like it to print out something with more significant figures like 158683269821 or something. Is there an option in sum() to specify how many sigfigs I want?

AI52487963
  • 1,253
  • 2
  • 17
  • 36
  • 2
    I just did this: `sum(158683269821,158683269821,158683269821 ,158683269821,158683269821)` and got this `793416349105`, so perhaps your answer really is exactly 1e12? – Christie Haskell Marsh Mar 07 '14 at 04:34
  • Weird. I get the same sum as you for that example, but for main question (I'm summing over the first two million primes) it seems unlikely to be exactly 1e12. If I do sum(x)+1 or sum(x)+1000 I get the same answer of 1e12, which is too imprecise for my purposes. – AI52487963 Mar 07 '14 at 04:37
  • 1
    Similar question: http://stackoverflow.com/questions/9397664/force-r-not-to-use-exponential-notation-e-g-e10 – Christie Haskell Marsh Mar 07 '14 at 04:40
  • 2
    what is `options("digits")`? Try setting `options(digits=7)` (I think that's the default setting), or `options(digits=20)` if you want to go crazy. – Ben Bolker Mar 07 '14 at 04:45
  • You were right about it being exactly 1e12, weirdly enough. The similar question helped for the precision too, thanks. – AI52487963 Mar 07 '14 at 04:45
  • You are encouraged to post an answer to your own question, if no-one else posts one, once a certain amount of time has elapsed (don't remember what the time limit is). – Ben Bolker Mar 07 '14 at 04:58

1 Answers1

2

The options I wound up using were thus:

> options("scipen"=100, "digits"=4)
> sum(x)
[1] 1000000000000
> sum(x)
[1] 1000000000000
> sum(x)+1
[1] 1000000000001
> sum(x)+2
[1] 1000000000002
> sum(x)-1
[1]  999999999999
AI52487963
  • 1,253
  • 2
  • 17
  • 36