Rev 54672 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/format.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2011 R Core Development Team% Copyright 2003-2011 The R Foundation% Distributed under GPL 2 or later\name{format}\alias{format}\alias{format.AsIs}\alias{format.data.frame}\alias{format.default}\alias{format.factor}\description{Format an \R object for pretty printing.}\title{Encode in a Common Format}\usage{format(x, \dots)\method{format}{default}(x, trim = FALSE, digits = NULL, nsmall = 0L,justify = c("left", "right", "centre", "none"),width = NULL, na.encode = TRUE, scientific = NA,big.mark = "", big.interval = 3L,small.mark = "", small.interval = 5L,decimal.mark = ".", zero.print = NULL, drop0trailing = FALSE, \dots)\method{format}{data.frame}(x, \dots, justify = "none")\method{format}{factor}(x, \dots)\method{format}{AsIs}(x, width = 12, \dots)}\arguments{\item{x}{any \R object (conceptually); typically numeric.}\item{trim}{logical; if \code{FALSE}, logical, numeric and complexvalues are right-justified to a common width: if \code{TRUE} theleading blanks for justification are suppressed.}\item{digits}{how many significant digits are to be used fornumeric and complex \code{x}. The default, \code{NULL}, uses\code{\link{getOption}(digits)}. This is a suggestion: enough decimalplaces will be used so that the smallest (in magnitude) number hasthis many significant digits, and also to satisfy \code{nsmall}.(For the interpretation for complex numbers see \code{\link{signif}}.)}\item{nsmall}{the minimum number of digits to the right of the decimalpoint in formatting real/complex numbers in non-scientific formats.Allowed values are \code{0 <= nsmall <= 20}.}\item{justify}{should a \emph{character} vector be left-justified (thedefault), right-justified, centred or left alone.}\item{width}{\code{default} method: the \emph{minimum} field width or\code{NULL} or \code{0} for no restriction.\code{AsIs} method: the \emph{maximum} field width for non-characterobjects. \code{NULL} corresponds to the default 12.}\item{na.encode}{logical: should \code{NA} strings be encoded? Notethis only applies to elements of character vectors, not to numericalor logical \code{NA}s, which are always encoded as \code{"NA"}.}% because Mr Gorjanc won't read, PR#12318\item{scientific}{Either a logical specifying whetherelements of a real or complex vector should be encoded in scientificformat, or an integer penalty (see \code{\link{options}("scipen")}).Missing values correspond to the current default penalty.}\item{\dots}{further arguments passed to or from other methods.}\item{big.mark, big.interval, small.mark,small.interval, decimal.mark, zero.print, drop0trailing}{%used for prettying (longish) decimal sequences, passed to\code{\link{prettyNum}}: that help page explains the details.}}\details{\code{format} is a generic function. Apart from the methods describedhere there are methods for dates (see \code{\link{format.Date}}),date-times (see \code{\link{format.POSIXct}})) and for other classes suchas \code{format.octmode} and \code{format.dist}.\code{format.data.frame} formats the data frame column by column,applying the appropriate method of \code{format} for each column.Methods for columns are often similar to \code{as.character} but offermore control. Matrix and data-frame columns will be converted toseparate columns in the result, and character columns (normally all)will be given class \code{"\link{AsIs}"}.\code{format.factor} converts the factor to a character vector andthen calls the default method (and so \code{justify} applies).\code{format.AsIs} deals with columns of complicated objects thathave been extracted from a data frame. Character objects are passedto the default method (and so \code{width} does not apply).Otherwise it calls \code{\link{toString}} to convert the objectto character (if a vector or list, element by element) and thenright-justifies the result.Justification for character vectors (and objects converted tocharacter vectors by their methods) is done on display width (see\code{\link{nchar}}), taking double-width characters and the renderingof special characters (as escape sequences, including escapingbackslash but not double quote: see \code{\link{print.default}}) intoaccount. Thus the width is as displayed by \code{print(quote =FALSE)} and not as displayed by \code{\link{cat}}. Character stringsare padded with blanks to the display width of the widest. (If\code{na.encode = FALSE} missing character strings are not included inthe width computations and are not encoded.)Numeric vectors are encoded with the minimum number of decimal placesneeded to display all the elements to at least the \code{digits}significant digits. However, if all the elements then have trailingzeroes, the number of decimal places is reduced until at least oneelement has a non-zero final digit; see also the argumentdocumentation for \code{big.*}, \code{small.*} etc, above. See thenote in \code{\link{print.default}} about \code{digits >= 16}.Raw vectors are converted to their 2-digit hexadecimal representationby \code{\link{as.character}}.}\value{An object of similar structure to \code{x} containing characterrepresentations of the elements of the first argument \code{x}in a common format, and in the current locale's encoding.For character, numeric, complex or factor \code{x}, dims and dimnamesare preserved on matrices/arrays and names on vectors: no otherattributes are copied.If \code{x} is a list, the result is a character vector obtained byapplying \code{format.default(x, \dots)} to each element of the list(after \code{\link{unlist}}ing elements which are themselves lists),and then collapsing the result for each element with\code{paste(collapse = ", ")}. The defaults in this case are\code{trim = TRUE, justify = "none"} since one does not usually wantalignment in the collapsed strings.}\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 an atomic vector would beformatted.\code{\link{formatC}}, \code{\link{paste}}, \code{\link{as.character}},\code{\link{sprintf}}, \code{\link{print}}, \code{\link{prettyNum}},\code{\link{toString}}, \code{\link{encodeString}}.}\examples{format(1:10)format(1:10, trim = TRUE)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)format(c(6.0, 13.1), digits = 2)format(c(6.0, 13.1), digits = 2, nsmall = 1)## use of scientificformat(2^31-1)format(2^31-1, scientific = TRUE)## a listz <- list(a=letters[1:3], b=(-pi+0i)^((-2:2)/2), c=c(1,10,100,1000),d=c("a", "longer", "character", "string"))format(z, digits = 2)format(z, digits = 2, justify = "left", trim = FALSE)}\keyword{character}\keyword{print}