[R] print format - fixed number of digits

From: Petr Klasterecky <klaster_at_karlin.mff.cuni.cz>
Date: Wed, 25 Apr 2007 16:32:17 +0200


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

______________________________________________
R-help_at_stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Wed 25 Apr 2007 - 15:13:08 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 25 Apr 2007 - 15:30:47 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.