Search code examples
rdigitsp-value

Wrong number of digits in format.pval()?


When specifying 3 digits in format.pval(), why does, say, 0.019950 outputs 4 digits:

format.pval(0.019950, eps=.001, digits=3, nsmall=3)
"0.0199"

But, say, 0.019951 outputs 3 digits:

format.pval(0.019951, eps=.001, digits=3, nsmall=3)
"0.020"

Edit (Solution):

The solution to having 3 digits while preserving the p-value formatting, based on dcarlson's answer, was simply to round the value to 3 digits before passing it to format.pval():

format.pval(round(0.019950, digits=3), eps=.001, digits=3, nsmall=3)
"0.020"

Solution

  • The digits= argument here and elsewhere in R is SIGNIFICANT digits, not decimal digits. Significant digits ignore leading 0's in a decimal so the first response is 3 significant digits since you do not count the 0 following the decimal point. In the second example the answer is rounded up to .02, but nsmall=3 forces the additional trailing 0.

    In addition, specifying a digits= argument in R is usually treated as advisory so it may not be followed. You can always force R to print the number of decimals using round or the formatting function sprintf.

    format.pval(round(0.019950, 3), digits=3, nsmall=3)
    # [1] "0.020"
    sprintf("%.3f", .019950)
    # [1] "0.020"