Format an R object for pretty printing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
format(x, ...) ## Default S3 method: format(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 = getOption("OutDec"), zero.print = NULL, drop0trailing = FALSE, ...) ## S3 method for class 'data.frame' format(x, ..., justify = "none") ## S3 method for class 'factor' format(x, ...) ## S3 method for class 'AsIs' format(x, width = 12, ...)
any R object (conceptually); typically numeric.
how many significant digits are to be used for
numeric and complex
the minimum number of digits to the right of the decimal
point in formatting real/complex numbers in non-scientific formats.
Allowed values are
should a character vector be left-justified (the default), right-justified, centred or left alone. Can be abbreviated.
Either a logical specifying whether
elements of a real or complex vector should be encoded in scientific
format, or an integer penalty (see
further arguments passed to or from other methods.
used for prettying (longish) numerical and complex sequences.
format is a generic function. Apart from the methods described
here there are methods for dates (see
format.POSIXct)) and for other classes such
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
format.factor converts the factor to a character vector and
then calls the default method (and so
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
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
account. Thus the width is as displayed by
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
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
small.* etc, above. See the
digits >= 16.
Raw vectors are converted to their 2-digit hexadecimal representation
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
An object of similar structure to
x containing character
representations of the elements of the first argument
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.
x is a list, the result is a character vector obtained by
format.default(x, ...) to each element of the list
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.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
format.info indicates how an atomic vector would be
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
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 nsmall format(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 scientific format(2^31-1) format(2^31-1, scientific = TRUE) ## a list z <- list(a = letters[1:3], b = (-pi+0i)^((-2:2)/2), c = c(1,10,100,1000), d = c("a", "longer", "character", "string"), q = quote( a + b ), e = expression(1+x)) ## can you find the "2" small differences? (f1 <- format(z, digits = 2)) (f2 <- format(z, digits = 2, justify = "left", trim = FALSE)) f1 == f2 ## 2 FALSE, 4 TRUE
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.