0

I can not find the answer to this question. All the answers my search engine gives me is how to round the number instead of how to get an unrounded number. Suppose I have a data.frame:

a <- c(1:4)
b <- c(1.123456789, 2.123456789, 3.123456789, 4.123456789)
df <- data.frame(a, b)

All methods I know return me a rounded number to the 6th digit after point:

df[2,2]
# [1] 2.123457
df[2,]
#   a        b
# 2 2 2.123457
df$b
# [1] 1.123457 2.123457 3.123457 4.123457
df$b[2]
# [1] 2.123457
df[df$a == 2, ]
#   a        b
# 2 2 2.123457

So, how to get the exact value? My desired output would be

[1] 2.123456789

Thank you!

Pavlo
  • 63
  • 6
  • 2
    Use `print` with digits `print(df[2, 2], digits = 16)# [1] 2.123456789` – akrun May 04 '22 at 18:47
  • Thank you! It works!!! I totally forgot this possibility. – Pavlo May 04 '22 at 18:50
  • 2
    `how to get the exact value` ... you are using floating point values which by definition are not exact. – Tim Biegeleisen May 04 '22 at 18:51
  • 2
    Note that there is a difference between how a number is *stored* and how it is *printed* on the console. For instance, see `pi`; now set `options(digits=19)` and then see `pi` again. The stored value never changed, just what is shown on the console. Another related option is `scipen=`; I encourage you to explore [`?options`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/options.html) to see the description of it and others. A key difference between `print(., digits=16)` and `options(digits=16)` is that the latter affects everything, not just the one call to `print` (until reset). – r2evans May 04 '22 at 18:57
  • 1
    Also, see https://stackoverflow.com/q/2287616/15293191 – AndrewGB May 04 '22 at 19:01
  • Thank you all for the profound comments! In practice I was needed to get only a single value which I was not able to see by `View(df)` – Pavlo May 04 '22 at 19:23
  • `df[2,2]` gives you the precise number. From there, it's `digits=` in whichever form. Is that it? – r2evans May 04 '22 at 20:23
  • 1
    You can also use `dput(df[2, 2])`. – dcarlson May 04 '22 at 21:00

0 Answers0