| formatN | R Documentation |
A simple utility getting more compact numeric representations, currently for “exponential” aka “scientific” format.
formatN(x, digits = 1L, sci = c(-1L, -2L))
x |
numeric or complex vector of numbers. |
digits |
positive integer number of digits to use. |
sci |
one or two small integers specifying
|
a character vector of the same length as x.
Martin Maechler
format, sprintf, formatC, are or
can be used similarly.
(x <- c(outer(c(1,pi), 10^(-10:11))))
sum(nchar(f1 <- format(x, digits=1))) # 220
sum(nchar(f2 <- sapply(x, format, digits=1)))# 194
sum(nchar(ff1<- formatN(x, sci=-1L))) # 152
sum(nchar(ff2<- formatN(x, sci=-2L))) # 150
sum(nchar(ff <- formatN(x))) # 150 .. the best
sum(nchar(ff3<- formatN(x, sci=-3L))) # 152
noquote(rbind(f1, f2, ff1, ff, ff2, ff3))
## the best is ff (where sci differs for neg.exp and pos.ex) - for this example
## the "extreme" formatting still keeps "95%" of numerical info :
stopifnot(all.equal(x, as.numeric(formatN(x)), tolerance = 0.05))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.