\Header{format}{Encode in a Common Format}
\methalias{format.AsIs}{format}
\methalias{format.data.frame}{format}
\methalias{format.default}{format}
\methalias{format.factor}{format}
\methalias{format.pval}{format}
\keyword{character}{format}
\keyword{print}{format}
\begin{Description}\relax
Format an \R{} object for pretty printing: \code{format.pval} is
intended for formatting p-values.\end{Description}
\begin{Usage}
\begin{verbatim}
format(x, ...)
format.AsIs(x, width = 12, ...)
format.data.frame(x, ..., justify = "none") 
format.default(x, trim = FALSE, digits = getOption("digits"),
               nsmall = 0, justify = c("left", "right", "none"), ...)
format.factor(x, ...)
format.pval(pv, digits = max(1, getOption("digits") - 2),
            eps = .Machine$double.eps, na.form = "NA")
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] any \R{} object (conceptually); typically numeric.
\item[\code{trim}] logical; if \code{TRUE}, leading blanks are trimmed off the
strings.
\item[\code{digits}] how many significant digits are to be used for
\code{\Link{numeric} x}. This is a suggestion: enough decimal
places will be used so that the smallest (in magnitude) number has
this many significant digits.
\item[\code{nsmall}] number of digits which will always appear to the right of
the decimal point in formatting real/complex numbers in
non-scientific formats. Allowed values \code{0 <= nsmall <= 20}.
\item[\code{justify}] should character vector be left-justified,
right-justified or left alone.  When justifying, the field width is
that of the longest string.
\item[\code{pv}] a numeric vector.
\item[\code{eps}] a numerical tolerance: see Details.
\item[\code{na.form}] character representation of \code{NA}s.
\item[\code{width}] the returned vector has elements of at most \code{width}.
\item[\code{...}] further arguments passed to or from other methods.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
These functions convert their first argument to a vector (or array) of
character 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 useful
when the strings are to be used for plot \code{\Link{axis}} annotation.

\code{format.AsIs} deals with columns of complicated objects that
have been extracted from a data frame.

\code{format.pval} is mainly an auxiliary function for
\code{\Link{print.summary.lm}} etc., does separate formatting for
fixed, floating point and very small values (those \code{< eps}).

The function \code{\Link{formatC}} provides a rather more flexible
formatting facility for numbers, but does \emph{not} provide a common
format 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.\end{Details}
\begin{Note}\relax
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{\bsl{}"}.\end{Note}
\begin{SeeAlso}\relax
\code{\Link{formatC}}, \code{\Link{paste}}, \code{\Link{as.character}}.\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
format(1:10)

zz <- data.frame("(row names)"= c("aaaaa", "b"), check.names=FALSE)
format(zz)
format(zz, justify="left")

## use of nsmall
format(13.7)
format(13.7, nsmall=3)

## handling of quotes
zz <- data.frame(a=I("abc"), b=I("def\"gh"))
format(zz)

p <- c(47,13,2,.1,.023,.0045, 1e-100)/1000
format.pval(p)
format.pval(p / 0.9)
format.pval(p / 0.9, dig=3)
\end{ExampleCode}
\end{Examples}

