Henrik Andersson
2005-May-30 14:43 UTC
[R] Formatting numbers with a limited amount of digits consistently
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! Is this a "feature" of R or did I miss something? --------------------------------------------- Henrik Andersson Netherlands Institute of Ecology - Centre for Estuarine and Marine Ecology P.O. Box 140 4400 AC Yerseke Phone: +31 113 577473 h.andersson at nioo.knaw.nl http://www.nioo.knaw.nl/ppages/handersson
Duncan Murdoch
2005-May-30 17:20 UTC
[R] Formatting numbers with a limited amount of digits consistently
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