[R] Formatting numbers with a limited amount of digits consistently
Gabor Grothendieck
ggrothendieck at gmail.com
Mon May 30 19:57:28 CEST 2005
On 5/30/05, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
> 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
>
That works in the above context but I don't think it works generally:
R> f <- head(faithful)
R> f
eruptions waiting
1 3.600 79
2 1.800 54
3 3.333 74
4 2.283 62
5 4.533 85
6 2.883 55
R> format(f, digits = 3)
eruptions waiting
1 3.60 79
2 1.80 54
3 3.33 74
4 2.28 62
5 4.53 85
6 2.88 55
R> # this works in this case
R> noquote(prettyNum(round(f,1), nsmall = 1))
eruptions waiting
[1,] 3.6 79.0
[2,] 1.8 54.0
[3,] 3.3 74.0
[4,] 2.3 62.0
[5,] 4.5 85.0
[6,] 2.9 55.0
and even that does not work in the desired way (which presumably
is not to use exponent format) if you have some
large enough numbers like 1e6 which it will display using
the e notation rather than using ordinary notation.
R> f[1,1] <- 1e6 + 0.11
R> noquote(prettyNum(round(f,1), nsmall = 1))
eruptions waiting
[1,] 1.0e+06 79.0
[2,] 1.8e+00 54.0
[3,] 3.3e+00 74.0
[4,] 2.3e+00 62.0
[5,] 4.5e+00 85.0
[6,] 2.9e+00 55.0
I have struggled with this myself and have generally been able
to come up with something for specific instances but I have generally
found it a pain to do a simple thing like format a table exactly as I want
without undue effort. Maybe someone else has figured this out.
More information about the R-help
mailing list