Movatterモバイル変換


[0]ホーム

URL:


rdrr.io

format: Encode in a Common Format

formatR Documentation

Encode in a Common Format

Description

Format anR object for pretty printing.

Usage

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

Arguments

x

anyR object (conceptually); typically numeric.

trim

logical; ifFALSE, logical, numeric and complexvalues are right-justified to a common width: ifTRUE theleading blanks for justification are suppressed.

digits

how many significant digits are to be used fornumeric and complexx. The default,NULL, usesgetOption("digits"). This is a suggestion: enough decimalplaces will be used so that the smallest (in magnitude) number hasthis many significant digits, and also to satisfynsmall.(For the interpretation for complex numbers seesignif.)

nsmall

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

justify

should acharacter vector be left-justified (thedefault), right-justified, centred or left alone. Can be abbreviated.

width

default method: theminimum field width orNULL or0 for no restriction.

AsIs method: themaximum field width for non-characterobjects.NULL corresponds to the default12.

na.encode

logical: shouldNA strings be encoded? Notethis only applies to elements of character vectors, not to numerical,complex nor logicalNAs, which are always encoded as"NA".

scientific

Either a logical specifying whetherelements of a real or complex vector should be encoded in scientificformat, or an integer penalty (seeoptions("scipen")).Missing values correspond to the current default penalty.

...

further arguments passed to or from other methods.

big.mark, big.interval, small.mark,small.interval, decimal.mark, zero.print, drop0trailing

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

Details

format is a generic function. Apart from the methods describedhere there are methods for dates (seeformat.Date),date-times (seeformat.POSIXct) and for other classes suchasformat.octmode andformat.dist.

format.data.frame formats the data frame column by column,applying the appropriate method offormat for each column.Methods for columns are often similar toas.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"AsIs".

format.factor converts the factor to a character vector andthen calls the default method (and sojustify applies).

format.AsIs deals with columns of complicated objects thathave been extracted from a data frame. Character objects and (atomic)matrices are passed to the default method (and sowidth doesnot apply).Otherwise it callstoString 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 (seenchar), taking double-width characters and the renderingof special characters (as escape sequences, including escapingbackslash but not double quote: seeprint.default) intoaccount. Thus the width is as displayed byprint(quote = FALSE) and not as displayed bycat. Character stringsare padded with blanks to the display width of the widest. (Ifna.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 thedigitssignificant digits. However, if all the elements then have trailingzeroes, the number of decimal places is reduced untilnsmall is reached or at least oneelement has a non-zero final digit; see also the argumentdocumentation forbig.*,small.* etc, above. See thenote inprint.default aboutdigits >= 16.

Raw vectors are converted to their 2-digit hexadecimal representationbyas.character.

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

The internal code respects the optiongetOption("OutDec") for the ‘decimal mark’, so ifthis is set to something other than"." then it takes precedenceover argumentdecimal.mark.

Value

An object of similar structure tox containing characterrepresentations of the elements of the first argumentxin a common format, and in the current locale's encoding.

For character, numeric, complex or factorx, dims and dimnamesare preserved on matrices/arrays and names on vectors: no otherattributes are copied.

Ifx is a list, the result is a character vector obtained byapplyingformat.default(x, ...) to each element of the list(afterunlisting elements which are themselves lists),and then collapsing the result for each element withpaste(collapse = ", "). The defaults in this case aretrim = 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)The New S Language.Wadsworth & Brooks/Cole.

See Also

format.info indicates how an atomic vector would beformatted.

formatC,paste,as.character,sprintf,print,prettyNum,toString,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"),          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## A "minimal" format() for S4 objects without their own format() method:cc <- methods::getClassDef("standardGeneric")format(cc) ## "<S4 class ......>"

What can we improve?

R Package Documentation

Browse R Packages

We want your feedback!

Note that we can't provide technical support on individual packages. You should contact the package authors for that.

 
Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, readEmbedding Snippets.

Close

[8]ページ先頭

©2009-2026 Movatter.jp