Arguments

logical; if FALSE, logical, numeric and complex
values are right-justified to a common width: if TRUE the
leading blanks for justification are suppressed.

digits

how many significant digits are to be used for
numeric and complex x. The default, NULL, uses
getOption("digits"). This is a suggestion: enough decimal
places will be used so that the smallest (in magnitude) number has
this many significant digits, and also to satisfy nsmall.
(For the interpretation for complex numbers see signif.)

nsmall

the minimum number of digits to the right of the decimal
point in formatting real/complex numbers in non-scientific formats.
Allowed values are 0 <= nsmall <= 20.

justify

should a character vector be left-justified (the
default), right-justified, centred or left alone. Can be abbreviated.

width

default method: the minimum field width or
NULL or 0 for no restriction.

AsIs method: the maximum field width for non-character
objects. NULL corresponds to the default 12.

na.encode

logical: should NA strings be encoded? Note
this only applies to elements of character vectors, not to numerical,
complex nor logical NAs, which are always encoded as "NA".

scientific

Either a logical specifying whether
elements of a real or complex vector should be encoded in scientific
format, or an integer penalty (see options("scipen")).
Missing values correspond to the current default penalty.

used for prettying (longish) numerical and complex sequences.
Passed to prettyNum: that help page explains the details.

Details

format is a generic function. Apart from the methods described
here there are methods for dates (see format.Date),
date-times (see format.POSIXct) and for other classes such
as format.octmode and format.dist.

format.data.frame formats the data frame column by column,
applying the appropriate method of format for each column.
Methods for columns are often similar to as.character but offer
more control. Matrix and data-frame columns will be converted to
separate columns in the result, and character columns (normally all)
will be given class "AsIs".

format.factor converts the factor to a character vector and
then calls the default method (and so justify applies).

format.AsIs deals with columns of complicated objects that
have been extracted from a data frame. Character objects and (atomic)
matrices are passed to the default method (and so width does
not apply).
Otherwise it calls toString to convert the object
to character (if a vector or list, element by element) and then
right-justifies the result.

Justification for character vectors (and objects converted to
character vectors by their methods) is done on display width (see
nchar), taking double-width characters and the rendering
of special characters (as escape sequences, including escaping
backslash but not double quote: see print.default) into
account. Thus the width is as displayed by print(quote =
FALSE) and not as displayed by cat. Character strings
are padded with blanks to the display width of the widest. (If
na.encode = FALSE missing character strings are not included in
the width computations and are not encoded.)

Numeric vectors are encoded with the minimum number of decimal places
needed to display all the elements to at least the digits
significant digits. However, if all the elements then have trailing
zeroes, the number of decimal places is reduced until
nsmall is reached or at least one
element has a non-zero final digit; see also the argument
documentation for big.*, small.* etc, above. See the
note in print.default about digits >= 16.

Raw vectors are converted to their 2-digit hexadecimal representation
by as.character.

format.default(x) now provides a “minimal” string when
isS4(x) is true.

The internal code respects the option
getOption("OutDec") for the ‘decimal mark’, so if
this is set to something other than "." then it takes precedence
over argument decimal.mark.

Value

An object of similar structure to x containing character
representations of the elements of the first argument x
in a common format, and in the current locale's encoding.

For character, numeric, complex or factor x, dims and dimnames
are preserved on matrices/arrays and names on vectors: no other
attributes are copied.

If x is a list, the result is a character vector obtained by
applying format.default(x, ...) to each element of the list
(after unlisting elements which are themselves lists),
and then collapsing the result for each element with
paste(collapse = ", "). The defaults in this case are
trim = TRUE, justify = "none" since one does not usually want
alignment in the collapsed strings.