sprintf                 package:base                 R Documentation

_U_s_e _C-_s_t_y_l_e _S_t_r_i_n_g _F_o_r_m_a_t_t_i_n_g _C_o_m_m_a_n_d_s

_D_e_s_c_r_i_p_t_i_o_n:

     A wrapper for the C function 'sprintf', that returns a character
     vector of length one containing a formatted combination of text
     and variable values.

_U_s_a_g_e:

     sprintf(fmt, ...)

_A_r_g_u_m_e_n_t_s:

     fmt: a format string.

     ...: values to be passed into 'fmt'. Only logical, integer, real
          and character vectors are accepted, and only the first value
          is read from each vector.

_D_e_t_a_i_l_s:

     This is a wrapper for the system 'sprintf' C-library function.  
     Attempts are made to check that the mode of the values passed
     match the format supplied, and R's special values ('NA', 'Inf',
     '-Inf' and 'NaN') are handled correctly.

     The following is abstracted from K&R (see References, below).  The
     string 'fmt' contains normal characters, which are passed through
     to the output string, and also special characters that operate on
     the arguments provided through '...'.  Special characters start
     with a '%' and terminate with one of the letters in the set
     'difeEgGs%'.  These letters denote the following types:

     '_d,_i' Integer value

     '_f' Double precision value, in decimal notation of the form
          "[-]mmm.ddd".  The number of decimal places is specified by
          the precision: the default is 6; a precision of 0 suppresses
          the decimal point.

     '_e,_E' Double precision value, in decimal notation of the form
          '[-]m.ddde[+-]xx' or '[-]m.dddE[+-]xx'

     '_g,_G' Double precision value, in '%e' or '%E' format if the
          exponent is less than -4 or greater than or equal to the
          precision, and '%f' format otherwise

     '_s' Character string

     '%' Literal '%' (none of the formatting characters given below are
          permitted in this case)

     In addition, between the initial '%' and the terminating
     conversion character there may be, in any order:

     '_m._n' Two numbers separated by a period, denoting the field width
          ('m') and the precision ('n')

     '-' Left adjustment of converted argument in its field

     '+' Always print number with sign

     _a _s_p_a_c_e Prefix a space if the first number is not a sign

     '_0' For numbers, pad to the field width with leading zeros

_V_a_l_u_e:

     A character vector of length one.  Character 'NA's are converted
     to '"NA"'.

_A_u_t_h_o_r(_s):

     Original code by Jonathan Rougier, J.C.Rougier@durham.ac.uk

_R_e_f_e_r_e_n_c_e_s:

     Kernighan, B. W. and Ritchie, D. M. (1988) _The C Programming
     Language._ Second edition, Prentice Hall. describes the format
     options in table B-1 in the Appendix.

_S_e_e _A_l_s_o:

     'formatC' for a way of formatting vectors of numbers in a similar
     fashion.

     'paste' for another way of creating a vector combining text and
     values.

_E_x_a_m_p_l_e_s:

     ## be careful with the format: most things in R are floats

     sprintf("%s is %f feet tall\n", "Sven", 7) # OK
     try(sprintf("%s is %i feet tall\n", "Sven", 7)) # not OK
     sprintf("%s is %i feet tall\n", "Sven", as.integer(7)) # OK again

     ## use a literal % :

     sprintf("%.0f%% said yes (out of a sample of size %.0f)", 66.666, 3)

     ## various formats of pi :

     sprintf("%f", pi)
     sprintf("%.3f", pi)
     sprintf("%1.0f", pi)
     sprintf("%5.1f", pi)
     sprintf("%05.1f", pi)
     sprintf("%+f", pi)
     sprintf("% f", pi)
     sprintf("%-10f", pi)# left justified
     sprintf("%e", pi)
     sprintf("%E", pi)
     sprintf("%g", pi)
     sprintf("%g",   1e6 * pi) # -> exponential
     sprintf("%.9g", 1e6 * pi) # -> "fixed"
     sprintf("%G", 1e-6 * pi)

     ## no truncation:
     sprintf("%1.f",101)

     ## More sophisticated:

     lapply(c("a", "ABC", "and an even longer one"),
            function(ch) sprintf("10-string '%10s'", ch))

     sapply(1:18, function(n)
            sprintf(paste("e with %2d digits = %.",n,"g",sep=""),
                    n, exp(1)))

