The “sprintf” Function in R

  • Package: Base R (No specific package, it’s a built-in function)

  • Purpose: To format and concatenate strings with the help of a format specification.

  • General Class: String Manipulation

  • Required Argument(s):

    • fmt: A format specification string.

    • …: Additional arguments to be formatted according to the format specification.

  • Notable Optional Arguments:

    • None

  • Example:

  • # Example usage
    name <- "John"
    age <- 30

    # Create a formatted string
    formatted_string <- sprintf("Hello, my name is %s and I am %d years old.", name, age)

    print(formatted_string)

  • In this example, sprintf is a built-in function in base R, and it is used to create a formatted string by substituting placeholders in the format specification string ("Hello, my name is %s and I am %d years old.") with the values of the variables name and age. The fmt argument is required, and additional arguments (name and age) are used for substitution. The result is a formatted string.

Previous
Previous

The “format” Function in R

Next
Next

The “substring” Function in R