R语言 打印一个格式化的字符串 – sprintf()函数
R语言中的 sprintf() 函数使用用户提供的格式,通过使用列表中的值返回格式化的字符串。
语法: sprintf(format, values)
参数:
format: 打印数值的格式
values: 要传入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"