[R] Automatic rounding of values after factors , converted to numeric, are multipled by a real number
Marc Schwartz (via MN)
mschwartz at mn.rr.com
Thu Oct 20 17:05:06 CEST 2005
On Thu, 2005-10-20 at 16:20 +0200, Peter Dalgaard wrote:
> "Nelson, Gary (FWE)" <Gary.Nelson at state.ma.us> writes:
>
> > Peter,
> >
> > Thank you for your response. I knew how close the values are to
> > integers, but I still don't understand why I don't have control over
> > how the numbers are displayed (rounded or not)?
>
> It's all in the conventions. The digits in print() and friends are
> significant digits, so we first round to that many significant digits,
> then discard trailing zeros, which is why
>
> > 12.500001
> [1] 12.5
> > 12.50001
> [1] 12.50001
>
> The exception is that we do not discard significant digits to the left
> of the decimal point, unless we are using scientific notation
>
> > print(12345678,digits=2)
> [1] 1.2e+07
> > print(12345678,digits=5)
> [1] 12345678
>
> (the "scipen" options controls the logic for switching notation).
>
> For finer control we have the formatC function:
>
> > format(1234.00001,digits=9) # same thing as with print()
> [1] "1234.00001"
> > format(1234.00001,digits=8)
> [1] "1234"
> > formatC(1234.00001,digits=5,format="f")
> [1] "1234.00001"
> > formatC(1234.00001,digits=4,format="f")
> [1] "1234.0000"
Also, sprintf():
> sprintf("%.9f", 1234.00001)
[1] "1234.000010000"
> sprintf("%.4f", 1234.00001)
[1] "1234.0000"
> sprintf("%12.4f", 1234.00001)
[1] " 1234.0000"
HTH,
Marc Schwartz
More information about the R-help
mailing list