-1

I'm trying to sum a vector of doubles in a data frame. When the sum is relatively low, this works as intended.

df <- data.frame(
  numbers = c(50, 632.5, 12.45)
)

sum(df$numbers)
# 694.95

But when the sum gets higher, the R begins to round the sum.

df <- data.frame(
  numbers = c(50000000, 632.5, 12.45)
)
sum(df$numbers)
# 50000645

How can I stop R from eliminating these decimal points? The output I want is:

sum(df$numbers)
# 50000645.95
Sean G
  • 385
  • 1
  • 3
  • 12
  • Note that you can encounter [floating point](http://stackoverflow.com/a/9508558/1412059) issues. You might need [arbitrary precision](https://cran.r-project.org/web/packages/Rmpfr/vignettes/Rmpfr-pkg.pdf) numbers. – Roland Nov 01 '16 at 14:00

2 Answers2

2

Try setting the number of digits in options, you can set whatever works for you. For example:

options(digits = 10)
sum(df$numbers)
[1] 50000644.95
Zach
  • 1,103
  • 8
  • 11
0

Alternatively, if you only want to change the settings for this computation, you could use:

    print(sum(df$numbers, digits = 10))
Antti
  • 91
  • 3