Re: [R] Formatting numbers with a limited amount of digits consistently

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Tue 31 May 2005 - 03:20:50 EST

Henrik Andersson wrote:
> I have tried to get signif, round and format to display numbers like
> these consistently in a table, using e.g. signif(x,digits=3)
>
> 17.01
> 18.15
>
> I want
>
> 17.0
> 18.2
>
> Not
>
> 17
> 18.2
>
>
> Why is the last digit stripped off in the case when it is zero!

signif() changes the value; you don't want that, you want to affect how a number is displayed. Use format() or formatC() instead, for example

 > x <- c(17.01, 18.15)
 > format(x, digits=3)
[1] "17.0" "18.1"
 > noquote(format(x, digits=3))
[1] 17.0 18.1

> Is this a "feature" of R or did I miss something?

I'd say both.

Duncan Murdoch



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue May 31 03:27:25 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:16 EST