3

A script from R Cookbook:

q <- seq(from=0,to=3,by=0.5)
tbl <- data.frame(Quant=q, Lower=pnorm(-q), Upper=pnorm(q))
print(tbl,digits=2)

results in:

  Quant Lower Upper 
1 0.0 0.5000 0.50 
2 0.5 0.3085 0.69 
3 1.0 0.1587 0.84 
4 1.5 0.0668 0.93
5 2.0 0.0228 0.98 
6 2.5 0.0062 0.99 
7 3.0 0.0013 1.00

Why is "Lower" different from others?

bitit1994
  • 362
  • 2
  • 4
  • 10
  • 1
    I guess that the option "digits=2" makes sure that the output contains at least two significant digits. In the example above the last entry of `Lower` is `0.0013`, with two significant (non-zero) digits. – RHertel Aug 14 '15 at 07:01
  • `?format` says for `digits` argument: *how many significant digits are to be used for numeric and complex x. The default, NULL, uses getOption("digits"). This is a suggestion: enough decimal places will be used so that the smallest (in magnitude) number has this many significant digits, and also to satisfy nsmall. (For the interpretation for complex numbers see signif.)*. In your case, `0.0013` is the smallest for `Lower`. It's significant digits are 4. –  Aug 14 '15 at 07:04
  • @Pascal The number of significant digits of 0.0013 is two, not four. This number can be written in scientific notation as 1.3e-3. – RHertel Aug 14 '15 at 08:01
  • 1
    OK. you need 4 number after . to get the 2 significant number. That's OK? –  Aug 14 '15 at 08:04
  • Yep :) Sorry if this was pedantic, but I thought it might be important to emphasize the meaning of "significant" digits here. – RHertel Aug 14 '15 at 08:05

1 Answers1

3

The option "digits=2" makes sure that the output of each columns contains at least two significant digits. In the example above the last entry of Lower is 0.0013, with two significant (non-zero) digits.

As an example, we can modify the option to digits=3:

> print(tbl,digits=3)
#   Quant  Lower Upper
#1   0.0 0.50000 0.500
#2   0.5 0.30854 0.691
#3   1.0 0.15866 0.841
#4   1.5 0.06681 0.933
#5   2.0 0.02275 0.977
#6   2.5 0.00621 0.994
#7   3.0 0.00135 0.999

Note that Lower in row 7 now has three significant digits. The other entries in that column are adapted accordingly.

If want to have an output that contains not more than two decimal digits for any number in the table, irrespective of the significance of the digits, you could use the round() function:

> round(tbl, digits=2)
#  Quant Lower Upper
#1   0.0  0.50  0.50
#2   0.5  0.31  0.69
#3   1.0  0.16  0.84
#4   1.5  0.07  0.93
#5   2.0  0.02  0.98
#6   2.5  0.01  0.99
#7   3.0  0.00  1.00
RHertel
  • 23,412
  • 5
  • 38
  • 64