0

I have a question according to the max function in R. I have a column in a dataframe which has 2 decimals after the comma and whenever I apply the max function on the column i will get the highest value but with only 1 decimal after the comma.

max(df$e)

How can I get two decimals after the comma? I tried using options() and round() but nothing works.

Reproducible example:

a = c(228, 239)
b = c(50,83)
d = c(0.27,0.24) 
e = c(2.12,1.69)
df = data.frame(a,b,d,e)

max(df$e)
#[1] 2.1

df
#   a    b    d   e
# 1 228 50 0.27 2.1
# 2 239 83 0.24 1.7

Now I would like to make more calculations:

df$f = (sqrt(df[,1]/(df[,2]+0.5))/max(df$e))*100

In the end the dataframe should have column a and b without decimals and d , e and f with two decimals after the comma.

Thank you!

  • `format(round(max(L$SQRT), 2), nsmall = 2)` – TarJae Jun 06 '22 at 17:09
  • That worked, but I wanted it as a numeric value to make further calculations with it. Now its a string. When I apply as.numeric i will get 2.1 again – Nesrin Othmann Jun 06 '22 at 17:11
  • `as.numeric(format(round(max(L$SQRT), 2), nsmall = 2))` – TarJae Jun 06 '22 at 17:13
  • When I apply as.numeric it is just one decimal after the comma instead of two – Nesrin Othmann Jun 06 '22 at 17:15
  • Please provide a minimal reproducible example – TarJae Jun 06 '22 at 17:17
  • If your column/values is numeric, then you printing options are the only thing controlling how many decimals will print. The data that is there is unchanged. If you want trailing zeroes to print, then you may need to convert it to a string for printing. – Gregor Thomas Jun 06 '22 at 17:39
  • But why for example is column `d` with two decimals and column `e` with one in the dataframe? – Nesrin Othmann Jun 06 '22 at 17:44
  • Hard to know - when I copy/paste your example in to my R session I see two digits for d and e, and 5 digits on f. Maybe show your `sessionInfo()` and `getOption("digits")`? Maybe restart R and see if you get the rounding in a fresh R session? – Gregor Thomas Jun 06 '22 at 18:15
  • 1
    I will say that "always 2 digits after the decimal point" isn't an option I know of in base R. You can experiment with, e.g., `print(df, digits = 3)`, `print(df, digits = 5)`. But if you want to have fine control over what prints, converting to strings with `format` or `formatC` is the way to get it, at the loss of your `numeric` class. – Gregor Thomas Jun 06 '22 at 18:20

1 Answers1

0

tl;dr you've probably got options(digits = 2).

df
    a  b    d    e
1 228 50 0.27 2.12
2 239 83 0.24 1.69

If I set options(digits = 2) then R uses this value to set the output format (it doesn't change the actual values), globally, to two significant digits — this is "total digits", not "digits after the decimal point".

options(digits = 2)
df
    a  b    d   e   f
1 228 50 0.27 2.1 100
2 239 83 0.24 1.7  80

To restore the value to the default, use options(digits = 7).

After restoring the default digits setting and computing df$f = (sqrt(df[,1]/(df[,2]+0.5))/max(df$e))*100 I get

    a  b    d    e        f
1 228 50 0.27 2.12 100.2273
2 239 83 0.24 1.69  79.8031

You can round the last column to two decimal places:

df$f <- round(df$f, 2)
> df
    a  b    d    e      f
1 228 50 0.27 2.12 100.23
2 239 83 0.24 1.69  79.80
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453