R sprintf()
> example(sprintf)
sprntf> ## be careful with the format: most things in R are floats
sprntf> ## only integer-valued reals get coerced to integer.
sprntf>
sprntf> sprintf("%s is %f feet tall\n", "Sven", 7.1) # OK
[1] "Sven is 7.100000 feet tall\n"
sprntf> 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) :
不正な書式 '%i'; 数値オブジェクトには %f, %e, %g または %a 書式を使ってください
sprntf> sprintf("%s is %i feet tall\n", "Sven", 7 ) # OK
[1] "Sven is 7 feet tall\n"
sprntf> ## use a literal % :
sprntf>
sprntf> 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)"
sprntf> ## various formats of pi :
sprntf>
sprntf> sprintf("%f", pi)
[1] "3.141593"
sprntf> sprintf("%.3f", pi)
[1] "3.142"
sprntf> sprintf("%1.0f", pi)
[1] "3"
sprntf> sprintf("%5.1f", pi)
[1] " 3.1"
sprntf> sprintf("%05.1f", pi)
[1] "003.1"
sprntf> sprintf("%+f", pi)
[1] "+3.141593"
sprntf> sprintf("% f", pi)
[1] " 3.141593"
sprntf> sprintf("%-10f", pi) # left justified
[1] "3.141593 "
sprntf> sprintf("%e", pi)
[1] "3.141593e+00"
sprntf> sprintf("%E", pi)
[1] "3.141593E+00"
sprntf> sprintf("%g", pi)
[1] "3.14159"
sprntf> sprintf("%g", 1e6 * pi) # -> exponential
[1] "3.14159e+06"
sprntf> sprintf("%.9g", 1e6 * pi) # -> "fixed"
[1] "3141592.65"
sprntf> sprintf("%G", 1e-6 * pi)
[1] "3.14159E-06"
sprntf> ## no truncation:
sprntf> sprintf("%1.f", 101)
[1] "101"
sprntf> ## re-use one argument three times, show difference between %x and %X
sprntf> xx <- sprintf("%1$d %1$x %1$X", 0:15)
sprntf> xx <- matrix(xx, dimnames = list(rep("", 16), "%d%x%X"))
sprntf> 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
sprntf> ## More sophisticated:
sprntf>
sprntf> sprintf("min 10-char string '%10s'",
sprntf+ c("a", "ABC", "and an even longer one"))
[1] "min 10-char string ' a'" "min 10-char string ' ABC'"
[3] "min 10-char string 'and an even longer one'"
sprntf> ## No test:
sprntf> ## Platform-dependent bad example from qdapTools 1.0.0:
sprntf> ## may pad with spaces or zeroes.
sprntf> sprintf("%09s", month.name)
[1] "00January" "0February" "0000March" "0000April" "000000May" "00000June" "00000July" "000August" "September"
[10] "00October" "0November" "0December"
sprntf> ## End(No test)
sprntf>
sprntf> n <- 1:18
sprntf> sprintf(paste0("e with %2d digits = %.", n, "g"), n, exp(1))
[1] "e with 1 digits = 3" "e with 2 digits = 2.7"
[3] "e with 3 digits = 2.72" "e with 4 digits = 2.718"
[5] "e with 5 digits = 2.7183" "e with 6 digits = 2.71828"
[7] "e with 7 digits = 2.718282" "e with 8 digits = 2.7182818"
[9] "e with 9 digits = 2.71828183" "e with 10 digits = 2.718281828"
[11] "e with 11 digits = 2.7182818285" "e with 12 digits = 2.71828182846"
[13] "e with 13 digits = 2.718281828459" "e with 14 digits = 2.718281828459"
[15] "e with 15 digits = 2.71828182845905" "e with 16 digits = 2.718281828459045"
[17] "e with 17 digits = 2.7182818284590451" "e with 18 digits = 2.71828182845904509"
sprntf> ## Using arguments out of order
sprntf> sprintf("second %2$1.0f, first %1$5.2f, third %3$1.0f", pi, 2, 3)
[1] "second 2, first 3.14, third 3"
sprntf> ## Using asterisk for width or precision
sprntf> sprintf("precision %.*f, width '%*.3f'", 3, pi, 8, pi)
[1] "precision 3.142, width ' 3.142'"
sprntf> ## Asterisk and argument re-use, 'e' example reiterated:
sprntf> sprintf("e with %1$2d digits = %2$.*1$g", n, exp(1))
[1] "e with 1 digits = 3" "e with 2 digits = 2.7"
[3] "e with 3 digits = 2.72" "e with 4 digits = 2.718"
[5] "e with 5 digits = 2.7183" "e with 6 digits = 2.71828"
[7] "e with 7 digits = 2.718282" "e with 8 digits = 2.7182818"
[9] "e with 9 digits = 2.71828183" "e with 10 digits = 2.718281828"
[11] "e with 11 digits = 2.7182818285" "e with 12 digits = 2.71828182846"
[13] "e with 13 digits = 2.718281828459" "e with 14 digits = 2.718281828459"
[15] "e with 15 digits = 2.71828182845905" "e with 16 digits = 2.718281828459045"
[17] "e with 17 digits = 2.7182818284590451" "e with 18 digits = 2.71828182845904509"
sprntf> ## re-cycle arguments
sprntf> sprintf("%s %d", "test", 1:3)
[1] "test 1" "test 2" "test 3"
sprntf> ## binary output showing rounding/representation errors
sprntf> x <- seq(0, 1.0, 0.1); y <- c(0,.1,.2,.3,.4,.5,.6,.7,.8,.9,1)
sprntf> 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"
この記事が気に入ったらサポートをしてみませんか?