How do I control the number of digits to display, say, in a matrix, without rounding or losing accuracy in subsequent calculations? round() of course reduces accuracy. Also, if all my results are really fractions, is there a way to display them as fractions of integers rather than decimal expressions? ................... judson blake [[alternative HTML version deleted]]
Dear Judson, Perhaps the following example would give you some ideas: R> options(digits = 4) R> rnorm(5) #[1] -0.57089 -0.14759 0.05717 -0.04935 2.22123 R> options(digits = 3) R> rnorm(5) #[1] 0.789 0.616 0.156 -1.315 -1.090 R> options(digits = 2) R> rnorm(5) #[1] -1.04 -0.58 -0.50 0.30 0.60 ?Best regards, Jorge.- On Mon, Oct 26, 2015 at 11:43 AM, Judson <judsonblake at msn.com> wrote:> How do I control the number of digits to display, > say, in a matrix, without rounding or losing accuracy > in subsequent calculations? > round() of course reduces accuracy. > > Also, if all my results > are really fractions, is there a way to display > them as fractions of integers rather than > decimal expressions? > > ................... judson blake > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
> myMatrix <- outer(c(1,24,30,75,96), 20:25, "/") > print(myMatrix, digits=3)[,1] [,2] [,3] [,4] [,5] [,6] [1,] 0.05 0.0476 0.0455 0.0435 0.0417 0.04 [2,] 1.20 1.1429 1.0909 1.0435 1.0000 0.96 [3,] 1.50 1.4286 1.3636 1.3043 1.2500 1.20 [4,] 3.75 3.5714 3.4091 3.2609 3.1250 3.00 [5,] 4.80 4.5714 4.3636 4.1739 4.0000 3.84> MASS::fractions(myMatrix)[,1] [,2] [,3] [,4] [,5] [,6] [1,] 1/20 1/21 1/22 1/23 1/24 1/25 [2,] 6/5 8/7 12/11 24/23 1 24/25 [3,] 3/2 10/7 15/11 30/23 5/4 6/5 [4,] 15/4 25/7 75/22 75/23 25/8 3 [5,] 24/5 32/7 48/11 96/23 4 96/25 Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Oct 26, 2015 at 9:43 AM, Judson <judsonblake at msn.com> wrote:> How do I control the number of digits to display, > say, in a matrix, without rounding or losing accuracy > in subsequent calculations? > round() of course reduces accuracy. > > Also, if all my results > are really fractions, is there a way to display > them as fractions of integers rather than > decimal expressions? > > ................... judson blake > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
> On Oct 26, 2015, at 11:43 AM, Judson <judsonblake at msn.com> wrote: > > How do I control the number of digits to display, > say, in a matrix, without rounding or losing accuracy > in subsequent calculations? > round() of course reduces accuracy. > > Also, if all my results > are really fractions, is there a way to display > them as fractions of integers rather than > decimal expressions? > > ................... judson blakeHi, You need to differentiate between the way in which R *stores* numeric values and the way in which it *displays* numeric values. If you want to affect the display of values in routine output, see ?options and note 'digits' and 'scipen'. Also see ?print.default. Those approaches do not affect the precision of calculations on the *stored* values. For fractions, see: require(MASS) ?fractions Regards, Marc Schwartz
On 26/10/2015 12:43 PM, Judson wrote:> How do I control the number of digits to display, > say, in a matrix, without rounding or losing accuracy > in subsequent calculations? > round() of course reduces accuracy. > > Also, if all my results > are really fractions, is there a way to display > them as fractions of integers rather than > decimal expressions?See the fractions() function in the MASS package. Duncan Murdoch
Just to elaborate on Marc's comment:> set.seed(42) > x <- runif(1) > dput(x) # What R is storing as x0.914806043496355> print(x) # Ways to display an approximation of x[1] 0.914806> print(x, digits=3)[1] 0.915> round(x, 2)[1] 0.91> signif(x, 4)[1] 0.9148> library(MASS) > fractions(x)[1] 7334/8017> dput(x) # But x is still the same0.914806043496355 David L. Carlson Department of Anthropology Texas A&M University -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Marc Schwartz Sent: Monday, October 26, 2015 2:40 PM To: Judson <judsonblake at msn.com> Cc: R-help <r-help at r-project.org> Subject: Re: [R] Number of digits to display?> On Oct 26, 2015, at 11:43 AM, Judson <judsonblake at msn.com> wrote: > > How do I control the number of digits to display, > say, in a matrix, without rounding or losing accuracy > in subsequent calculations? > round() of course reduces accuracy. > > Also, if all my results > are really fractions, is there a way to display > them as fractions of integers rather than > decimal expressions? > > ................... judson blakeHi, You need to differentiate between the way in which R *stores* numeric values and the way in which it *displays* numeric values. If you want to affect the display of values in routine output, see ?options and note 'digits' and 'scipen'. Also see ?print.default. Those approaches do not affect the precision of calculations on the *stored* values. For fractions, see: require(MASS) ?fractions Regards, Marc Schwartz ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.