The sprintf() function is used to print formatted strings in R.
The following method shows how you can do it with syntax.
Method 1: Use sprintf() Function
sprintf(format,string)
The following example show how to use sprintf() method in R:
Use sprintf() Function to Print Formatted String
Let’s see how we can use sprintf() function in R to print formatted string:
# Declare string
str <- "I am %s. I am %d years old and having experience of %0.1f years."
# Declare values
name <- "Richa"
age <- 40
exp <- 15.5
# Show formatted string
print(sprintf(str,name,age,exp))
Output:
[1] "123.59"
Here in the above code we define string and values then format string by adding values to it.
In the str() function, we used different placeholder like %s for string, %d for integer and %f for float.