在 R 编程中打印格式化字符串– sprintf()函数
R 语言中的sprintf()
函数使用用户提供的格式来返回格式化字符串,并使用列表中的值。
Syntax: sprintf(format, values)
Parameter:
format: Format of printing the values
values: to be passed into format
示例 1:
# R program to illustrate
# the use of sprintf() function
# Initializing values
x1 <- "Welcome"
x2 <- "GeeksforGeeks"
# Calling sprintf() function
sprintf("% s to % s", x1, x2)
输出:
[1] "Welcome to GeeksforGeeks"
示例 2:
# R program to illustrate
# the use of sprintf() function
# Initializing values
x1 <- "GeeksforGeeks"
x2 <- 100
x3 <- "success"
# Calling sprintf() function
sprintf("% s gives %.0f percent % s", x1, x2, x3)
输出:
[1] "GeeksforGeeks gives 100 percent success"