Examples for 'base::sprintf'


Use C-style String Formatting Commands

Aliases: sprintf gettextf

Keywords: print character

### ** Examples

## be careful with the format: most things in R are floats
## only integer-valued reals get coerced to integer.

sprintf("%s is %f feet tall\n", "Sven", 7.1)      # OK
[1] "Sven is 7.100000 feet tall\n"
try(sprintf("%s is %i feet tall\n", "Sven", 7.1)) # not OK
Error in sprintf("%s is %i feet tall\n", "Sven", 7.1) : 
  invalid format '%i'; use format %f, %e, %g or %a for numeric objects
    sprintf("%s is %i feet tall\n", "Sven", 7  )  # OK
[1] "Sven is 7 feet tall\n"
## use a literal % :

sprintf("%.0f%% said yes (out of a sample of size %.0f)", 66.666, 3)
[1] "67% said yes (out of a sample of size 3)"
## various formats of pi :

sprintf("%f", pi)
[1] "3.141593"
sprintf("%.3f", pi)
[1] "3.142"
sprintf("%1.0f", pi)
[1] "3"
sprintf("%5.1f", pi)
[1] "  3.1"
sprintf("%05.1f", pi)
[1] "003.1"
sprintf("%+f", pi)
[1] "+3.141593"
sprintf("% f", pi)
[1] " 3.141593"
sprintf("%-10f", pi) # left justified
[1] "3.141593  "
sprintf("%e", pi)
[1] "3.141593e+00"
sprintf("%E", pi)
[1] "3.141593E+00"
sprintf("%g", pi)
[1] "3.14159"
sprintf("%g",   1e6 * pi) # -> exponential
[1] "3.14159e+06"
sprintf("%.9g", 1e6 * pi) # -> "fixed"
[1] "3141592.65"
sprintf("%G", 1e-6 * pi)
[1] "3.14159E-06"
## no truncation:
sprintf("%1.f", 101)
[1] "101"
## re-use one argument three times, show difference between %x and %X
xx <- sprintf("%1$d %1$x %1$X", 0:15)
xx <- matrix(xx, dimnames = list(rep("", 16), "%d%x%X"))
noquote(format(xx, justify = "right"))
 %d%x%X
  0 0 0
  1 1 1
  2 2 2
  3 3 3
  4 4 4
  5 5 5
  6 6 6
  7 7 7
  8 8 8
  9 9 9
 10 a A
 11 b B
 12 c C
 13 d D
 14 e E
 15 f F
## More sophisticated:

sprintf("min 10-char string '%10s'",
        c("a", "ABC", "and an even longer one"))
[1] "min 10-char string '         a'"            
[2] "min 10-char string '       ABC'"            
[3] "min 10-char string 'and an even longer one'"
## No test: 
## Platform-dependent bad example from qdapTools 1.0.0:
## may pad with spaces or zeroes.
sprintf("%09s", month.name)
 [1] "  January" " February" "    March" "    April" "      May" "     June"
 [7] "     July" "   August" "September" "  October" " November" " December"
## End(No test)

n <- 1:18
sprintf(paste0("e with %2d digits = %.", n, "g"), n, exp(1))
 [1] "e with  1 digits = 3"                  
 [2] "e with  2 digits = 2.7"                
 [3] "e with  3 digits = 2.72"               
 [4] "e with  4 digits = 2.718"              
 [5] "e with  5 digits = 2.7183"             
 [6] "e with  6 digits = 2.71828"            
 [7] "e with  7 digits = 2.718282"           
 [8] "e with  8 digits = 2.7182818"          
 [9] "e with  9 digits = 2.71828183"         
[10] "e with 10 digits = 2.718281828"        
[11] "e with 11 digits = 2.7182818285"       
[12] "e with 12 digits = 2.71828182846"      
[13] "e with 13 digits = 2.718281828459"     
[14] "e with 14 digits = 2.718281828459"     
[15] "e with 15 digits = 2.71828182845905"   
[16] "e with 16 digits = 2.718281828459045"  
[17] "e with 17 digits = 2.7182818284590451" 
[18] "e with 18 digits = 2.71828182845904509"
## Using arguments out of order
sprintf("second %2$1.0f, first %1$5.2f, third %3$1.0f", pi, 2, 3)
[1] "second 2, first  3.14, third 3"
## Using asterisk for width or precision
sprintf("precision %.*f, width '%*.3f'", 3, pi, 8, pi)
[1] "precision 3.142, width '   3.142'"
## Asterisk and argument re-use, 'e' example reiterated:
sprintf("e with %1$2d digits = %2$.*1$g", n, exp(1))
 [1] "e with  1 digits = 3"                  
 [2] "e with  2 digits = 2.7"                
 [3] "e with  3 digits = 2.72"               
 [4] "e with  4 digits = 2.718"              
 [5] "e with  5 digits = 2.7183"             
 [6] "e with  6 digits = 2.71828"            
 [7] "e with  7 digits = 2.718282"           
 [8] "e with  8 digits = 2.7182818"          
 [9] "e with  9 digits = 2.71828183"         
[10] "e with 10 digits = 2.718281828"        
[11] "e with 11 digits = 2.7182818285"       
[12] "e with 12 digits = 2.71828182846"      
[13] "e with 13 digits = 2.718281828459"     
[14] "e with 14 digits = 2.718281828459"     
[15] "e with 15 digits = 2.71828182845905"   
[16] "e with 16 digits = 2.718281828459045"  
[17] "e with 17 digits = 2.7182818284590451" 
[18] "e with 18 digits = 2.71828182845904509"
## re-cycle arguments
sprintf("%s %d", "test", 1:3)
[1] "test 1" "test 2" "test 3"
## binary output showing rounding/representation errors
x <- seq(0, 1.0, 0.1); y <- c(0,.1,.2,.3,.4,.5,.6,.7,.8,.9,1)
cbind(x, sprintf("%a", x), sprintf("%a", y))
      x                                                  
 [1,] "0"   "0x0p+0"               "0x0p+0"              
 [2,] "0.1" "0x1.999999999999ap-4" "0x1.999999999999ap-4"
 [3,] "0.2" "0x1.999999999999ap-3" "0x1.999999999999ap-3"
 [4,] "0.3" "0x1.3333333333334p-2" "0x1.3333333333333p-2"
 [5,] "0.4" "0x1.999999999999ap-2" "0x1.999999999999ap-2"
 [6,] "0.5" "0x1p-1"               "0x1p-1"              
 [7,] "0.6" "0x1.3333333333334p-1" "0x1.3333333333333p-1"
 [8,] "0.7" "0x1.6666666666667p-1" "0x1.6666666666666p-1"
 [9,] "0.8" "0x1.999999999999ap-1" "0x1.999999999999ap-1"
[10,] "0.9" "0x1.ccccccccccccdp-1" "0x1.ccccccccccccdp-1"
[11,] "1"   "0x1p+0"               "0x1p+0"              

[Package base version 4.2.3 Index]