Hi! Does anyone know hot to set number of digits to be printed in function 'paste'? Tine Mla?
For detailed control of such things see ?sprintf and ?formatC S?ren -----Oprindelig meddelelse----- Fra: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] P? vegne af Tine Sendt: 18. juni 2008 10:21 Til: r-help at r-project.org Emne: [R] Number of digits in paste funciton Hi! Does anyone know hot to set number of digits to be printed in function 'paste'? Tine Mla? ______________________________________________ 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.
Tine wrote:> Hi! > > Does anyone know hot to set number of digits to be printed in function > 'paste'? >Basically, you can't, because as.character always uses 15 digits (chopping trailing zeros). Instead, use round(), format(), formatC(), etc. Or sprintf()> paste(format(pi,digits=4),format(exp(1),digits=6), sep="+")[1] "3.142+2.71828"> sprintf(fmt="%.4f+%.6f",pi,exp(1))[1] "3.1416+2.718282" -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
On Wed, 18 Jun 2008, Tine wrote:> Hi! > > Does anyone know hot to set number of digits to be printed in function > 'paste'?paste() does not print, so we can only guess at what you mean. The help says 'paste' converts its arguments (_via_ 'as.character') to character strings, and concatenates them (separating them by the string given by 'sep'). So I guess you are talking about conversion by as.character. And its help says 'as.character' represents real and complex numbers to 15 significant digits (technically the compiler's setting of the ISO C constant 'DBL_DIG', which will be 15 on machines supporting IEC60559 arithmetic according to the C99 standard). This ensures that all the digits in the result will be reliable (and not the result of representation error), but does mean that conversion to character and back to numeric may change the number. If you want to convert numbers to character with the maximum possible precision, use 'format'. and format() seems to be what you too are looking for (or sprintf or formatC).> > Tine Mla? > > ______________________________________________ > 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. >PLEASE do at least read the help before posting, as the posting guides asks of you. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Maybe Matching Threads
- Incorrect paste() output (PR#10801)
- Different value between R variance and definition of variance
- Problem with lsa package (data.frame) on Windows XP
- how to reconstruct the discriminant funciton from lda$prior, $means and $scaling
- Inverse Gaussian distribution not working in glm funciton