Rev 27716 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{format}\title{Encode in a Common Format}\usage{format(x, \dots)\method{format}{AsIs}(x, width = 12, \dots)\method{format}{data.frame}(x, \dots, justify = "none")\method{format}{default}(x, trim = FALSE, digits = NULL,nsmall = 0, justify = c("left", "right", "none"),big.mark = "", big.interval = 3,small.mark = "", small.interval = 5,decimal.mark = ".", \dots)\method{format}{factor}(x, \dots)format.pval(pv, digits = max(1, getOption("digits") - 2),eps = .Machine$double.eps, na.form = "NA")prettyNum(x, big.mark = "", big.interval = 3,small.mark = "", small.interval = 5,decimal.mark = ".", \dots)}\alias{format}\alias{format.AsIs}\alias{format.data.frame}\alias{format.default}\alias{format.factor}\alias{format.pval}\alias{prettyNum}\description{Format an \R object for pretty printing: \code{format.pval} isintended for formatting p-values.}\arguments{\item{x}{any \R object (conceptually); typically numeric.}\item{trim}{logical; if \code{TRUE}, leading blanks are trimmed off thestrings.}\item{digits}{how many significant digits are to be used for\code{\link{numeric} x}. The default, \code{NULL}, uses\code{\link{options}()$digits}. This is a suggestion: enough decimalplaces will be used so that the smallest (in magnitude) number hasthis many significant digits.}\item{nsmall}{number of digits which will always appear to the right ofthe decimal point in formatting real/complex numbers innon-scientific formats. Allowed values \code{0 <= nsmall <= 20}.}\item{justify}{should character vector be left-justified,right-justified or left alone. When justifying, the field width isthat of the longest string.}\item{big.mark}{character; if not empty used as mark between every\code{big.interval} decimals \emph{before} (hence \code{big}) thedecimal point.}\item{big.interval}{see \code{big.mark} above; defaults to 3.}\item{small.mark}{character; if not empty used as mark between every\code{small.interval} decimals \emph{after} (hence \code{small}) thedecimal point.}\item{small.interval}{see \code{small.mark} above; defaults to 5.}\item{decimal.mark}{the character used to indicate the numericdecimal point.}\item{pv}{a numeric vector.}\item{eps}{a numerical tolerance: see Details.}\item{na.form}{character representation of \code{NA}s.}\item{width}{the returned vector has elements of at most \code{width}.}\item{\dots}{further arguments passed to or from other methods.}}\details{These functions convert their first argument to a vector (or array) ofcharacter strings which have a common format (as is done by\code{\link{print}}), fulfilling \code{length(format*(x, *)) ==length(x)}. The trimming with \code{trim = TRUE} is usefulwhen the strings are to be used for plot \code{\link[graphics]{axis}} annotation.\code{format.AsIs} deals with columns of complicated objects thathave been extracted from a data frame.\code{format.pval} is mainly an auxiliary function for\code{\link[stats]{print.summary.lm}} etc., and does separate formatting forfixed, floating point and very small values; those less than\code{eps} are formatted as \code{"< [eps]"} (where \dQuote{[eps]}stands for \code{format(eps, digits)}.The function \code{\link{formatC}} provides a rather more flexibleformatting facility for numbers, but does \emph{not} provide a commonformat for several numbers, nor it is platform-independent.\code{format.data.frame} formats the data frame column by column,applying the appropriate method of \code{format} for each column.\code{prettyNum} is the utility function for prettifying \code{x}. If\code{x} is not a character, \code{format(x[i], ...)} is applied toeach element, and then it is left unchanged if all the other argumentsare at their defaults. Note that \code{prettyNum(x)} may behaveunexpectedly if \code{x} is a \code{character} not resulting fromsomething like \code{format(<number>)}.}\note{Currently \code{format} drops trailing zeroes, so \code{format(6.001,digits=2)} gives \code{"6"} and \code{format(c(6.0, 13.1),digits=2)} gives \code{c(" 6", "13")}.Character(s) \code{"} in input strings \code{x} are escaped to \code{\\"}.}\references{Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth \& Brooks/Cole.}\seealso{\code{\link{format.info}} indicates how something would be formatted;\code{\link{formatC}}, \code{\link{paste}}, \code{\link{as.character}},\code{\link{sprintf}}.}\examples{format(1:10)zz <- data.frame("(row names)"= c("aaaaa", "b"), check.names=FALSE)format(zz)format(zz, justify="left")## use of nsmallformat(13.7)format(13.7, nsmall=3)r <- c("76491283764.97430", "29.12345678901", "-7.1234", "-100.1","1123")## American:prettyNum(r, big.mark = ",")## Some Europeans:prettyNum(r, big.mark = "'", decimal.mark = ",")(dd <- sapply(1:10, function(i)paste((9:0)[1:i],collapse="")))prettyNum(dd, big.mark="'")pN <- stats::pnorm(1:7, lower=FALSE)cbind(format (pN, small.mark = " ", digits = 15))cbind(formatC(pN, small.mark = " ", digits = 17, format = "f"))}\keyword{character}\keyword{print}