[R] Rounding and printing

Karl Ove Hufthammer karl at huftis.org
Thu Oct 29 17:56:00 CET 2009


On Thu, 29 Oct 2009 12:29:42 -0400 Alan Cohen <CohenA at smh.toronto.on.ca> 
wrote:
> I am trying to print a table with numbers all rounded to the same 
> number of digits (one after the decimal), but R seems to want to not 
> print ".0" for integers.

'round' only rounds numbers; it doesn't format them. Use 'formatC' 
instead. Example:

> x=c(1.234, 20.31, 25, 0.7)
> formatC(x, digits=1, format="f")
[1] "1.2"  "20.3" "25.0" "0.7" 

'prettyNum' may also be useful.

-- 
Karl Ove Hufthammer




More information about the R-help mailing list