[R] specify the number of decimal numbers
Wacek Kusnierczyk
Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Thu May 14 14:27:40 CEST 2009
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
More information about the R-help
mailing list