R语言 打印一个格式化的字符串 – sprintf()函数

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)
R

输出

[1] "Welcome to GeeksforGeeks"
R

例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)
R

输出

[1] "GeeksforGeeks gives 100 percent success"
R

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册