| 1 |
\name{formatN} |
| 2 |
\alias{formatN} |
| 3 |
\title{Compact Numeric Formatting} |
| 4 |
\description{ |
| 5 |
A simple utility getting more compact numeric representations, currently |
| 6 |
for \dQuote{exponential} aka \dQuote{scientific} format. |
| 7 |
} |
| 8 |
\usage{ |
| 9 |
formatN(x, digits = 1L, sci = c(-1L, -2L)) |
| 10 |
} |
| 11 |
\arguments{ |
| 12 |
\item{x}{numeric or complex vector of numbers.} |
| 13 |
\item{digits}{positive integer number of digits to use.} |
| 14 |
\item{sci}{one or two small integers specifying |
| 15 |
\code{\link{format.default}}'s \code{scientific} argument (see |
| 16 |
\code{\link{options}("scipen")}) for \code{abs(x) < 1} and \eqn{\ge 1}{>= 1}. |
| 17 |
Note that the default \code{(-2. -1)} seems optimal; this argument |
| 18 |
exists mostly to explore possibilities and to prove optimality of its |
| 19 |
default.} |
| 20 |
} |
| 21 |
\value{ |
| 22 |
a \code{\link{character}} vector of the same length as \code{x}. |
| 23 |
} |
| 24 |
\author{Martin Maechler} |
| 25 |
\seealso{ |
| 26 |
\code{\link{format}}, \code{\link{sprintf}}, \code{\link{formatC}}, are or |
| 27 |
can be used similarly. |
| 28 |
} |
| 29 |
\examples{ |
| 30 |
(x <- c(outer(c(1,pi), 10^(-10:11)))) |
| 31 |
sum(nchar(f1 <- format(x, digits=1))) # 220 |
| 32 |
sum(nchar(f2 <- sapply(x, format, digits=1)))# 194 |
| 33 |
sum(nchar(ff1<- formatN(x, sci=-1L))) # 152 |
| 34 |
sum(nchar(ff2<- formatN(x, sci=-2L))) # 150 |
| 35 |
sum(nchar(ff <- formatN(x))) # 150 .. the best |
| 36 |
sum(nchar(ff3<- formatN(x, sci=-3L))) # 152 |
| 37 |
noquote(rbind(f1, f2, ff1, ff, ff2, ff3)) |
| 38 |
## the best is ff (where sci differs for neg.exp and pos.ex) - for this example |
| 39 |
|
| 40 |
## the "extreme" formatting still keeps "95%" of numerical info : |
| 41 |
stopifnot(all.equal(x, as.numeric(formatN(x)), tolerance = 0.05)) |
| 42 |
} |
| 43 |
\keyword{utilities} |