0

Ich have a numeric value (x1=0.01) which I want to format with two significant digits:

> p_digits <- 2
> x1 <- 0.01
> str(x1)
  num 0.01
> formatC(x1, format = "fg", digits = p_digits, flag = "#", big.mark = "'")

which gives:

[1] "0.010"

Now, I retrieve the same value from a dataframe with structure

> str(df0)
'data.frame':   328 obs. of  5 variables:
 $ analyse   : chr  "ALAT/GPT" "ALAT/GPT" "Albumin" "Albumin" ...
 $ min       : num  NA 1 35 1 0 0 0 0 36 1 ...
 $ max       : num  35 5000 55 200 20 NA 30 7000 92 3000 ...

> x2 <- subset(df0, analyse == "somewhat")$min
> x2
[1] 0.01
> str(x2)
  num 0.01

Now, if I format x2 I get:

> formatC(x2, format = "fg", digits = p_digits, flag = "#", big.mark = "'")
[1] "0.0100"    

In summary, I have two numeric parameters x1 and x2 with both the same value 0.01 which gives me different outputs using formatC.

Update 1: The dataframe was built by importing a MySQL table with ODBC.

Update 2: Different results depending from figure with small deviation

> x3 <- 0.0101111
> formatC(x3, format = "fg", digits = 2, flag = "#", big.mark = "'")
[1] "0.010"

> x4 <- 0.0099999
> formatC(x4, format = "fg", digits = 2, flag = "#", big.mark = "'")
[1] "0.0100"

For me it is not clear why x4 gives 3 significant digits.

giordano
  • 2,954
  • 7
  • 35
  • 57
  • 1
    is `all.equal(x1, x2)` `TRUE` ? – Ronak Shah Aug 21 '19 at 07:36
  • Good point. It gives me: `"Mean relative difference: 2.235174e-08"`. – giordano Aug 21 '19 at 09:40
  • If I round with `round(x2,2)` I got the result I want: `formatC(round(x2,2), format = "fg", digits = 2, flag = "#", big.mark = "'")` gives `0.010`. Nevertheless, for me it is not clear why this happend, see update 2. – giordano Aug 21 '19 at 09:52

1 Answers1

1

Have you changed p_digits in the meanwhile? At least I can not reproduce your observations.

p_digits  <- 2

x1 <- 0.01
str(x1)
formatC(x1, format = "fg", digits = p_digits, flag = "#", big.mark = "'")
#[1] "0.010"

df0  <- data.frame(analyse=c("somewhat","a","b"), min=c(.01, 9000, .01234))
x2 <- subset(df0, analyse == "somewhat")$min
formatC(x2, format = "fg", digits = p_digits, flag = "#", big.mark = "'")
#[1] "0.010"

x3 <- 0.0101111
formatC(x3, format = "fg", digits = 2, flag = "#", big.mark = "'")
#[1] "0.010"

x4 <- 0.0099999
formatC(x4, format = "fg", digits = 2, flag = "#", big.mark = "'")
#[1] "0.010"

CHANGES IN R 3.6.0: Calls like formatC(*, zero.print = "< 0.001") no longer give an error and are further improved via new optional argument replace.zero. Reported by David Hugh-Jones.

GKi
  • 37,245
  • 2
  • 26
  • 48
  • Sorry, I missed p_digits. The problem may be that x1 and x2 are not identical. See comment of Ronak Shah and my update 1. – giordano Aug 21 '19 at 09:44
  • @giordano I still can not reproduce your output. Different R-Versions? (3.6.1) – GKi Aug 21 '19 at 10:01
  • I have version R version 3.6.0 (2019-04-26). Thanks! I will try the new vesion as soon as possible. – giordano Aug 21 '19 at 10:14
  • I installed version `R version 3.6.1 (2019-07-05)` but still I get `"0.0100"` with `x4=0.009999`. – giordano Aug 21 '19 at 21:36
  • I at least get "0.010". Maybe then this comes from the C-library? I compile R under `x86_64-pc-linux-gnu` Debian 10. – GKi Aug 26 '19 at 06:09
  • @giordano If I run `x4 <- 0.0099999; formatC(x4, format = "fg", digits = 2, flag = "#", big.mark = "'")` on the console at https://www.rdocumentation.org/packages/base/versions/3.6.1/topics/formatC it also returns `"0.010"`. – GKi Aug 27 '19 at 15:20