Hi, How can I format numbers to engineering notation? That is, like scientific but where the exponent is always a multiple of three. Some examples: 1635 000 000 => 1.635E9 163 500 000 => 163.5E6 0.000 000 000 135 => 135E-9 I've tried format and formatC but I couldn't get them to work they I want. Any help is greatly appreciated. Thanks, Sam
Both format and formatC (and sprintf) use C facilities for scientific format. As far as I know C has no facilities for your desired format, so you would need to write your own C code and interface to R. On Thu, 6 Jul 2006, Walker, Sam wrote:> Hi, > > How can I format numbers to engineering notation? > > That is, like scientific but where the exponent is always a multiple of > three. > > Some examples: > > 1635 000 000 => 1.635E9 > 163 500 000 => 163.5E6 > 0.000 000 000 135 => 135E-9 > > I've tried format and formatC but I couldn't get them to work they I > want. > > Any help is greatly appreciated. > > > Thanks, > Sam > > ______________________________________________ > 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 >-- 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
Hi, try this: formatEng <- function(x) { s<-as.numeric(strsplit(format(x, scientific=T),"e")[[1]]) return(paste(s[1]*10^(s[2]%%3),as.integer(s[2]-(s[2]%%3)),sep="e")) }>> >> Some examples: >> >> 1635 000 000 => 1.635E9 >> 163 500 000 => 163.5E6 >> 0.000 000 000 135 != 135E-9 >> 0.000 000 000 135 => 125E-12 ?Hans ********************************************************** Hans-Joerg Bibiko Max Planck Institute for Evolutionary Anthropology Department of Linguistics Deutscher Platz 6 phone: +49 (0) 341 3550 341 D-04103 Leipzig fax: +49 (0) 341 3550 333 Germany e-mail: bibiko at eva.mpg.de
Clever... Good observation on the last example, mistake on my part. Thanks for all the suggestions. Sam -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Hans-Joerg Bibiko Sent: Friday, July 07, 2006 4:27 AM To: r-help at stat.math.ethz.ch Subject: Re: [R] engineering notation format Hi, try this: formatEng <- function(x) { s<-as.numeric(strsplit(format(x, scientific=T),"e")[[1]]) return(paste(s[1]*10^(s[2]%%3),as.integer(s[2]-(s[2]%%3)),sep="e")) }>> >> Some examples: >> >> 1635 000 000 => 1.635E9 >> 163 500 000 => 163.5E6 >> 0.000 000 000 135 != 135E-9 >> 0.000 000 000 135 => 125E-12 ?Hans ********************************************************** Hans-Joerg Bibiko Max Planck Institute for Evolutionary Anthropology Department of Linguistics Deutscher Platz 6 phone: +49 (0) 341 3550 341 D-04103 Leipzig fax: +49 (0) 341 3550 333 Germany e-mail: bibiko at eva.mpg.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
Hi, One week ago Sam Walker asked for a way to format a number to the engineering notation? After my suggestion I received many mails how to use this function formatEng within plots dealing, e.g., with physics and SI prefixes. So, I wrote a humble wiki article about that. If you are interested have a look at http://wiki.r-project.org/rwiki/doku.php?id=tips:data- strings:formatengineering Comments are welcome! Cheers, Hans-Joerg