format
Formatted Character Data

Description

Coerces the input to character strings using a common format. This function is useful for building custom output displays and often is used in conjunction with cat to print such displays.

Usage

format(x, ...)

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, ...)

format.factor(x, ...)

format.data.frame(x, ..., justify = "none")

Arguments

x an object of a class that has a method for format, or an atomic object. Missing values (NA) are allowed.
trim a logical flag. If FALSE (the default), the formatted numbers have the same length. Note that this flag does not affect formatting of character strings (see "justify").
digits the maximum number of (significant) digits to use.
nsmall the minimum number of digits to appear to the right of the decimal.
justify a character string giving the justification of character strings relative to each other. The choices are "none", "left", "right" and "centre". Only the first letter needs to be given. Note that this argument does not affect formatting of numbers (see "trim").
width the minimum width of the formatted strings.
na.encode a logical flag: if FALSE, character NAs are kept as NA. If TRUE, characters NAs are encoded as the string "NA".
scientific a logical flag: if TRUE, numbers are always formatted using scientific notation. If FALSE, numbers are never formatted using scientific notation. If NA, a heuristic is used to determine whether numbers are represented in scientific notation.
big.mark the mark with which to group the digits to the left of the decimal.
big.interval the number of digits in a group to the left of the decimal.
small.mark the character used to group digits to the right of the decimal.
small.interval the number of digits in a group to the right of the decimal.
decimal.mark the character to put at the decimal place (between the ones place and the one-tenths place).
zero.print a logical flag. If FALSE, any item in x that is zero prints as a space. A character string or NULL is allowed.
drop0trailing a logical flag. If TRUE, indicates any trailing zero should be removed.

Details

This is a generic function.
If given the single argument x, format.default takes all elements of x and converts them to a character form using a common format, just as they would be printed by the print function. This ensures that all elements of the value have the same number of characters and have decimal places aligned.
format.factor(x,...) implements the format generic for factor objects. It returns a character vector of levels factor that factor elements point to. Equivalent to as.character(x).
format.data.frame(x,...) implements the format generic for data frame objects. It returns a data frame where all values are formatted into character strings.
Value
returns an object similar to the input, but converted to a character.
Differences between Spotfire Enterprise Runtime for R and Open-source R
Calling format.data.frame with zero columns returns a zero-column data.frame with the same row names (hence number of rows) as the input data.frame. In open-source R, this call returns an error.
See Also
cat, paste, print, seq, write, prettyNum.
Examples
format(2^seq(10)) 
#  [1] "   2" "   4" "   8" "  16" "  32" 
#  [6] "  64" " 128" " 256" " 512" "1024" 

Package base version 6.1.1-7
Package Index