Hi, I would like to use the format function to get numbers all with three digits to the right of the decimal point, even in cases where there is no significant digit left. For example, I would like to get c(0.3456789,0.0000053) as "0.346" "0.000". It seems that it is not possible to force format to print a "0.000", i.e. without any significant decimal places. Is it possible to do this somehow in R? Thanks, Christian *********************************************************************** Christian Hennig Fachbereich Mathematik-SPST/ZMS, Universitaet Hamburg hennig at math.uni-hamburg.de, http://www.math.uni-hamburg.de/home/hennig/ ####################################################################### ich empfehle www.boag-online.de
On Mon, 17 Jan 2005 16:49:35 +0100 (MET) Christian Hennig wrote:> Hi, > > I would like to use the format function to get numbers all with three > digits to the right of the decimal point, even in cases where there is > no significant digit left. For example, I would like to get > c(0.3456789,0.0000053) as "0.346" "0.000". > It seems that it is not possible to force format to print a "0.000", > i.e. without any significant decimal places. > Is it possible to do this somehow in R?You can round() before: R> x <- c(0.3456789,0.0000053) R> round(x, digits = 3) [1] 0.346 0.000 R> format(round(x, digits = 3)) [1] "0.346" "0.000" hth, Z> Thanks, > Christian > > > ********************************************************************* > ** Christian Hennig > Fachbereich Mathematik-SPST/ZMS, Universitaet Hamburg > hennig at math.uni-hamburg.de, > http://www.math.uni-hamburg.de/home/hennig/ > ##################################################################### > ### > ich empfehle www.boag-online.de > > ______________________________________________ > 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 Mon, Jan 17, 2005 at 04:49:35PM +0100, Christian Hennig wrote:> Hi, > > I would like to use the format function to get numbers all with three > digits to the right of the decimal point, even in cases where there is no > significant digit left. For example, I would like to get > c(0.3456789,0.0000053) as "0.346" "0.000". > It seems that it is not possible to force format to print a "0.000", i.e. > without any significant decimal places. > Is it possible to do this somehow in R?sprintf can do that > sprintf("%3.3f %3.3f", 0.3456789, 0.00000053) [1] "0.346 0.000" You'd have to loop over your vector to do it one by one, I think. Hth, Dirk -- Better to have an approximate answer to the right question than a precise answer to the wrong question. -- John Tukey as quoted by John Chambers
Christian Hennig wrote:>I would like to use the format function to get numbers all with three >digits to the right of the decimal point, even in cases where there is no >significant digit left. For example, I would like to get >c(0.3456789,0.0000053) as "0.346" "0.000".I posted a fairly complete example of doing this some time ago. If you search the list archives under my name, you should be able to find it. It's not at hand right now, or I would post it again. The key was using formatC(), but my example had other little issues worked out as well. MHP -- Michael H. Prager, Ph.D. Population Dynamics Team NOAA Center for Coastal Habitat and Fisheries Research NMFS Southeast Fisheries Science Center Beaufort, North Carolina 28516 USA http://shrimp.ccfhrb.noaa.gov/~mprager/