Hi, I was wondering how to specify the number of decimal numbers in my computation using R? I have too many decimal numbers for my result, when I convert them to string with as.character, the string will be too long. Thanks and regards! -- View this message in context: http://www.nabble.com/specify-the-number-of-decimal-numbers-tp23538852p23538852.html Sent from the R help mailing list archive at Nabble.com.
Depending on what you want to do, use 'sprintf':> x <- 1.23456789 > x[1] 1.234568> as.character(x)[1] "1.23456789"> sprintf("%.1f %.3f %.5f", x,x,x)[1] "1.2 1.235 1.23457">On Thu, May 14, 2009 at 7:40 AM, lehe <timlee126@yahoo.com> wrote:> > Hi, > I was wondering how to specify the number of decimal numbers in my > computation using R? I have too many decimal numbers for my result, when I > convert them to string with as.character, the string will be too long. > Thanks and regards! > -- > View this message in context: > http://www.nabble.com/specify-the-number-of-decimal-numbers-tp23538852p23538852.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
On 14-May-09 11:40:21, lehe wrote:> Hi, > I was wondering how to specify the number of decimal numbers in my > computation using R? I have too many decimal numbers for my result, > when I convert them to string with as.character, the string will > be too long. > Thanks and regards!Since you say you want them to end up as character strinngs, it would seem that sprintf() is the ideal tool for the job. 1. Read ?sprintf 2. Examples: pi # [1] 3.141593 sprintf("%.3f",pi) # [1] "3.142" # sprintf("%10.3f",pi) [1] " 3.142" sprintf("%010.3f",pi) # [1] "000003.142" sprintf("%025.17f",pi) # [1] "0000003.14159265358979312" So you can do pretty much what you want, in terms of output format. Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 14-May-09 Time: 13:04:30 ------------------------------ XFMail ------------------------------