[R] specify the number of decimal numbers
(Ted Harding)
Ted.Harding at manchester.ac.uk
Thu May 14 15:47:32 CEST 2009
On 14-May-09 12:27:40, Wacek Kusnierczyk wrote:
> jim holtman wrote:
>> Depending on what you want to do, use 'sprintf':
>>
>>> x <- 1.23456789
>>> x
>> [1] 1.234568
>>
>>> as.character(x)
>> [1] "1.23456789"
>>
>>> sprintf("%.1f %.3f %.5f", x,x,x)
>> [1] "1.2 1.235 1.23457"
>>
> ... but remember that sprintf introduces excel bugs into r (i.e.,
> rounding is not done according to the IEC 60559 standard, see ?round):
>
> ns = c(0.05, 0.15)
> round(ns, 1)
> # 0.0 0.2
> as.numeric(sprintf('%.1f', ns))
> # 0.1 0.1
> vQ
True! And thanks for importing that point into the discussion.
And, by the way, it goes some way to solving an issue I raised
earlier, in that
M
# [,1] [,2]
# [1,] 3.141593 9.424778
# [2,] 6.283185 12.566371
round(10*M,3)
# [,1] [,2]
# [1,] 31.416 94.248
# [2,] 62.832 125.664
though, of course, still
round(1.0,3)
# [1] 1 ### not 1.000
It would still be good to be able to simply have
print(X,decimals=3)
(with proper rounding, of course)
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 14-May-09 Time: 14:47:30
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list