R语言 把向量的元素转换成字符串 – toString()函数

R语言 把向量的元素转换成字符串 – toString()函数

R 语言 中的toString()函数 用于产生一个描述R对象的单字符字符串。

语法: toString(x, width = NULL)

参数

  • x: R对象
  • width: 建议的最大字段宽度。NULL或0的值表示没有最大值。接受的最小值是6,更小的值被视为6

toString() 函数在R语言中的例子

例1:R语言中toString()函数的基本例子

# R program to illustrate
# toString function
 
# Initializing a string vector
x <- c("GFG", "Geeks", "GeeksforGeekss")
 
# Calling the toString() function
toString(x)
R

输出:

[1] "GFG, Geeks, GeeksforGeekss"
R

例2: 用R语言中的toString()函数进行格式化

# R program to illustrate
# toString function
 
# Initializing a string vector
x <- c("GFG", "Geeks", "GeeksforGeekss")
 
# Calling the toString() function
toString(x, width = 2)
toString(x, width = 8)
toString(x, width = 10)
R

输出:

[1] "GF...."
[1] "GFG, ...."
[1] "GFG, G...."
R

例3:在R中把矩阵转换为字符串

# Matrix having 3 rows and 3 columns
# filled by a single constant 5
mat <- (matrix(5, 3, 3))
print(mat)
str <- toString(mat)
print("String")
print(str)
R

输出:

     [,1] [,2] [,3]
[1,]    5    5    5
[2,]    5    5    5
[3,]    5    5    5
[1] "String"
[1] "5, 5, 5, 5, 5, 5, 5, 5, 5"
R

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册