Creating strings from variables

    Solution

    The two common ways of creating strings from variables are the function and the sprintf function. paste is more useful for vectors, and sprintf is more useful for precise control of the output.

    Another way is to use sprintf function. This is derived from the function of the same name in the C programming language.

    For integers, use %d or a variant:

    For floating-point numbers, use %f for standard notation, and %e or %E for exponential notation. You can also use or %G for a “smart” formatter that automatically switches between the two formats, depending on where the significant digits are. The following examples are taken from the R help page for sprintf:

    Other miscellaneous things:

    See for more information about output in scripts.