Examples for 'base::format'


Encode in a Common Format

Aliases: format format.AsIs format.data.frame format.default format.factor

Keywords: character print

### ** Examples

format(1:10)
 [1] " 1" " 2" " 3" " 4" " 5" " 6" " 7" " 8" " 9" "10"
format(1:10, trim = TRUE)
 [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10"
zz <- data.frame("(row names)"= c("aaaaa", "b"), check.names = FALSE)
format(zz)
  (row names)
1       aaaaa
2           b
format(zz, justify = "left")
  (row names)
1       aaaaa
2       b    
## use of nsmall
format(13.7)
[1] "13.7"
format(13.7, nsmall = 3)
[1] "13.700"
format(c(6.0, 13.1), digits = 2)
[1] " 6" "13"
format(c(6.0, 13.1), digits = 2, nsmall = 1)
[1] " 6.0" "13.1"
## use of scientific
format(2^31-1)
[1] "2147483647"
format(2^31-1, scientific = TRUE)
[1] "2.147484e+09"
## 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))
                                                             a 
                                                     "a, b, c" 
                                                             b 
"-0.32+0.00i, 0.00-0.56i, 1.00+0.00i, 0.00+1.77i, -3.14+0.00i" 
                                                             c 
                                            "1, 10, 100, 1000" 
                                                             d 
                  "a        , longer   , character, string   " 
                                                             q 
                                                       "a + b" 
                                                             e 
                                           "expression(1 + x)" 
(f2 <- format(z, digits = 2, justify = "left", trim = FALSE))
                                                                a 
                                                        "a, b, c" 
                                                                b 
"-0.32+0.00i,  0.00-0.56i,  1.00+0.00i,  0.00+1.77i, -3.14+0.00i" 
                                                                c 
                                         "   1,   10,  100, 1000" 
                                                                d 
                     "a        , longer   , character, string   " 
                                                                q 
                                                          "a + b" 
                                                                e 
                                              "expression(1 + x)" 
f1 == f2 ## 2 FALSE, 4 TRUE
    a     b     c     d     q     e 
 TRUE FALSE FALSE  TRUE  TRUE  TRUE 
## A "minimal" format() for S4 objects without their own format() method:
cc <- methods::getClassDef("standardGeneric")
format(cc) ## "<S4 class ......>"
[1] "<S4 class 'classRepresentation' [package \"methods\"] with 11 slots>"

[Package base version 4.2.3 Index]