[R] print format - fixed number of digits

Petr Klasterecky klaster at karlin.mff.cuni.cz
Wed Apr 25 16:32:17 CEST 2007


Hi,

is it possible to format real (double) numbers in a data frame to an 
exact format?
I need something like format(..., digits=5) but this is a suggestion 
only and I need exactly 5 digits. Example where there are more than 5 
digits printed follows.

I can do it via
cbind(as.numeric(sprintf("%.5f",column_1)), 
as.numeric(sprintf("%.5f",column_2)),...)
but it is really annoying and there must be an easier solution.

Petr

 > x <- as.data.frame(matrix(rnorm(6),nrow=3)/100)

 > x
             V1           V2
1  0.002640759 -0.002335782
2 -0.003960130  0.010373135
3 -0.007079349 -0.005792717

 > format(x,digits=5,scientific=FALSE)
           V1         V2
1  0.0026408 -0.0023358
2 -0.0039601  0.0103731
3 -0.0070793 -0.0057927
 >


-- 
Petr Klasterecky
Dept. of Probability and Statistics
Charles University in Prague
Czech Republic



More information about the R-help mailing list