[R] Automatic rounding of values after factors , converted to numeric, are multipled by a real number

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Oct 20 16:20:10 CEST 2005


"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"


-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907




More information about the R-help mailing list