Hi all R-users, If I have a matrix with numeric elements as follows, the values are rounded when I try to refer to a specifici element using [], the value is rounded. The same thing happens if the matrix is read from a file, the values are stored to the correct precision but then when I try to refer to a specific element (such as using [], it is rounded. How do I avoid this rounding? >mdat<-matrix(c(0.0187972950,0.4446208550,1.0000000000,0.0003204380,0.0105002420,1.1087556380,0.0742164230,0.0362898240), nrow = 2, ncol=4) > mdat [,1] [,2] [,3] [,4] [1,] 0.01879729 1.000000000 0.01050024 0.07421642 [2,] 0.44462085 0.000320438 1.10875564 0.03628982 Thanks.
The signif() function doesn't increase the number significant digits beyond the default. However, if your purpose is simply to see more digits (or what was entered), a paste() function will do the trick though it will trim the trailing zeros. Other ways? Tim ---- Original message ---->Date: Thu, 06 Jan 2005 18:57:03 -0500 >From: Ulas Karaoz <ulask at bu.edu> >Subject: [R] How to avoid rounding of matrix elements? >To: R-help at stat.math.ethz.ch > >Hi all R-users, >If I have a matrix with numeric elements as follows, thevalues are>rounded when I try to refer to a specifici element using[], the value>is rounded. >The same thing happens if the matrix is read from a file,the values are>stored to the correct precision but then when I try torefer to a>specific element (such as using [], it is rounded. > >How do I avoid this rounding? > >mdat<-matrix(c(0.0187972950,0.4446208550,1.0000000000,0.0003204380,0.010500 2420,1.1087556380,0.0742164230,0.0362898240),>nrow = 2, ncol=4) > > mdat > [,1] [,2] [,3] [,4] >[1,] 0.01879729 1.000000000 0.01050024 0.07421642 >[2,] 0.44462085 0.000320438 1.10875564 0.03628982 > >Thanks. > >______________________________________________ >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
It's not being rounded, as this example will show:> x <- 0.1234567898765 > x[1] 0.1234568> print(x,digits=15)[1] 0.1234567898765 It is being printed, or displayed, with fewer digits. But the underlying value has not changed. -Don At 6:57 PM -0500 1/6/05, Ulas Karaoz wrote:>Hi all R-users, >If I have a matrix with numeric elements as follows, the values are >rounded when I try to refer to a specifici element using [], the >value is rounded. >The same thing happens if the matrix is read from a file, the values >are stored to the correct precision but then when I try to refer to >a specific element (such as using [], it is rounded. > >How do I avoid this rounding? > >mdat<-matrix(c(0.0187972950,0.4446208550,1.0000000000,0.0003204380,0.0105002420,1.1087556380,0.0742164230,0.0362898240), >nrow = 2, ncol=4) >> mdat > [,1] [,2] [,3] [,4] >[1,] 0.01879729 1.000000000 0.01050024 0.07421642 >[2,] 0.44462085 0.000320438 1.10875564 0.03628982 > >Thanks. > >______________________________________________ >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-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA
Use the digits option to print():> print(mdat, digits=20)[,1] [,2] [,3] [,4] [1,] 0.0187972949999999990 1.000000000 0.0105002420000000 0.074216423 [2,] 0.4446208549999999800 0.000320438 1.1087556380000001 0.036289824 The output may look rounded, but they're not. Andy> From: Ulas Karaoz > > Hi all R-users, > If I have a matrix with numeric elements as follows, the values are > rounded when I try to refer to a specifici element using [], > the value > is rounded. > The same thing happens if the matrix is read from a file, the > values are > stored to the correct precision but then when I try to refer to a > specific element (such as using [], it is rounded. > > How do I avoid this rounding? > > >mdat<-matrix(c(0.0187972950,0.4446208550,1.0000000000,0.00032 > 04380,0.0105002420,1.1087556380,0.0742164230,0.0362898240), > nrow = 2, ncol=4) > > mdat > [,1] [,2] [,3] [,4] > [1,] 0.01879729 1.000000000 0.01050024 0.07421642 > [2,] 0.44462085 0.000320438 1.10875564 0.03628982 > > Thanks. > > ______________________________________________ > 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 > >
On Thu, 2005-01-06 at 18:57 -0500, Ulas Karaoz wrote:> Hi all R-users, > If I have a matrix with numeric elements as follows, the values are > rounded when I try to refer to a specifici element using [], the > value > is rounded. > The same thing happens if the matrix is read from a file, the values > are > stored to the correct precision but then when I try to refer to a > specific element (such as using [], it is rounded. > > How do I avoid this rounding? > >mdat<-matrix(c > (0.0187972950,0.4446208550,1.0000000000,0.0003204380,0.0105002420,1.1087556380,0.0742164230,0.0362898240), > nrow = 2, ncol=4) > > mdat > [,1] [,2] [,3] [,4] > [1,] 0.01879729 1.000000000 0.01050024 0.07421642 > [2,] 0.44462085 0.000320438 1.10875564 0.03628982 > > Thanks.When you display the matrix, you are using a print method to do so. In this case print.matrix() is being used. If you review the help for the function (or ?print.default), you will see that there is an argument called 'digits' which, if not explicitly defined, is set to options("digits"), which by default is 7. Note importantly, that this has _nothing_ to do with the fashion in which the data is being stored internally, which is by default a 'double' precision float. This only affects how the data is displayed. The digits option, when printing a multi-element structure, is the _minimum_ number of significant digits that will be printed. If you want additional control over the number of decimal places that is printed, you can do this in several ways: # Explicitly use the print function> print(mdat, digits = 9)[,1] [,2] [,3] [,4] [1,] 0.018797295 1.000000000 0.010500242 0.074216423 [2,] 0.444620855 0.000320438 1.108755638 0.036289824 # Increase 'digits' globally> options(digits = 9) > mdat[,1] [,2] [,3] [,4] [1,] 0.018797295 1.000000000 0.010500242 0.074216423 [2,] 0.444620855 0.000320438 1.108755638 0.036289824 # Use a format family function> formatC(mdat, format = "f", digits = 12)[,1] [,2] [,3] [1,] "0.018797295000" "1.000000000000" "0.010500242000" [2,] "0.444620855000" "0.000320438000" "1.108755638000" [,4] [1,] "0.074216423000" [2,] "0.036289824000" See: ?print.default ?options ?formatC ?format ?sprintf for more information. HTH, Marc Schwartz
Dear Ulas, A number of people have suggested changing the print precision via the digits argument to print(), and have explained that what you see when a number is printed doesn't necessarily reflect the precision of the stored number. You can also reset the digits option, which defaults to 7. (You'll still lose trailing zeroes, however.) For you example:> options(digits=10) > mdat[,1] [,2] [,3] [,4] [1,] 0.018797295 1.000000000 0.010500242 0.074216423 [2,] 0.444620855 0.000320438 1.108755638 0.036289824 I hope this helps, John -------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 http://socserv.mcmaster.ca/jfox --------------------------------> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ulas Karaoz > Sent: Thursday, January 06, 2005 6:57 PM > To: R-help at stat.math.ethz.ch > Subject: [R] How to avoid rounding of matrix elements? > > Hi all R-users, > If I have a matrix with numeric elements as follows, the > values are rounded when I try to refer to a specifici element > using [], the value is rounded. > The same thing happens if the matrix is read from a file, the > values are stored to the correct precision but then when I > try to refer to a specific element (such as using [], it is rounded. > > How do I avoid this rounding? > > >mdat<-matrix(c(0.0187972950,0.4446208550,1.0000000000,0.00032 > 04380,0.0105002420,1.1087556380,0.0742164230,0.0362898240), > nrow = 2, ncol=4) > > mdat > [,1] [,2] [,3] [,4] > [1,] 0.01879729 1.000000000 0.01050024 0.07421642 [2,] > 0.44462085 0.000320438 1.10875564 0.03628982 > > Thanks. > > ______________________________________________ > 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