[R] Help with format()

Michael Karol MKarol at syntapharma.com
Fri Aug 19 16:23:49 CEST 2011


R Users:

  Can anyone please help me with the following:
  I'm unclear as to how to get format to do what I want.
  I've tried the following and get unexpected results.

Input.....
val<-321.6

format(val, digits=1)
format(val, digits=2) 
format(val, digits=3)
format(val, digits=4)
format(val, digits=5)

Output....

[1] "322"
[1] "322"
[1] "322"
[1] "321.6"
[1] "321.6"

Whereas I would expect to get...

[1] "300"
[1] "320"
[1] "322"
[1] "321.6"
[1] "321.60"

since "digits" is defined as the number of "significant digits".  The number 321.6 shown to 1 significant digit should be 300., not 322 which is 3 significant digits!  Likewise for the other cases.

Can anyone explain what format() is doing? 


Regards, 
Michael



More information about the R-help mailing list