[R] Controlling the precision of the digits printed
Kevin Burton
rkevinburton at charter.net
Tue Nov 15 18:08:06 CET 2011
Thank you. I mainly didn't know about the vector/matrix printing rules.
Kevin
-----Original Message-----
From: William Dunlap [mailto:wdunlap at tibco.com]
Sent: Tuesday, November 15, 2011 10:43 AM
To: Kevin Burton; r-help at r-project.org
Subject: RE: [R] Controlling the precision of the digits printed
When you print a vector R uses a single
format for the whole vector and tries to come up with one format that
displays all the values accurately enough. For a matrix (or data.frame) it
uses a different format for each column, so perhaps you would like the
output of:
> matrix(a, nrow=1, dimnames=list("", names(a)))
A B C D E F
1e-10 1 2 3 0.5 0.25
Now you said you wanted a minimum of 4 digits after the decimal point for
"large fractions" like 0.25 but only 2 when using scientific notation for
"small fractions" like 1.0e-10 and you didn't say what you wanted for big
numbers like pi*10^10. That rule seems complicated enough that you may want
to write your own print function based on sprintf().
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Kevin Burton
> Sent: Tuesday, November 15, 2011 8:19 AM
> To: r-help at r-project.org
> Subject: [R] Controlling the precision of the digits printed
>
> Has anyone come across the right combinations to print a limited
> number of digits? My trial and error approach is taking too much time.
> Here is what I have tried:
>
>
>
> > op <- options()
>
> > a <- c(1e-10,1,2,3,.5,.25)
>
> > names(a) <- c("A", "B", "C", "D", "E", "F")
>
> > # default
>
> > a
>
> A B C D E F
>
> 1.0e-10 1.0e+00 2.0e+00 3.0e+00 5.0e-01 2.5e-01
>
> > options(digits = 4, scipen=5)
>
> > # Doesn't print exponents but there are too many trailing digits
>
> > a
>
> A B C D E
> F
>
> 0.0000000001 1.0000000000 2.0000000000 3.0000000000 0.5000000000
> 0.2500000000
>
>
>
> > options(digits = 3, scipen=4)
>
> > # Now we are back to exponents
>
> > a
>
> A B C D E F
>
> 1.0e-10 1.0e+00 2.0e+00 3.0e+00 5.0e-01 2.5e-01
>
>
>
> I would like the integers to print as integers (1,2,3). The larger
> fractions to print something like .5000 or .2500. And the very small
> number to use exponents (1.0e-10)
>
>
>
> Is this possible?
>
>
>
> Thank you.
>
>
>
> Kevin
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.
More information about the R-help
mailing list