R语言 把参数打印到屏幕上 – print()函数
R语言中的 print() 函数是用来将参数打印到屏幕上的。
语法: print(x, digits, na.print)
参数:
x: 要显示的指定参数
digits: 定义最小有效数字
na.print: 表示NA值的输出格式
例1 :
# R program to illustrate
# print function
# Creating a data frame
x <- cars[1:5, ]
# Calling the print() function
# to print the above data frame
print(x)
输出
speed dist
1 4 2
2 4 10
3 7 4
4 7 22
5 8 16
例2 :
# R program to illustrate
# print function
# Initializing a number
x <- 15 / 7
# Calling the print() function
# using digits parameter
print(x, digits = 2)
print(x, digits = 3)
print(x, digits = 4)
输出
[1] 2.1
[1] 2.14
[1] 2.143
例3 :
# R program to illustrate
# print function
# Creating a matrix
x <- matrix(c(2, NA, 5, NA, 8, NA, 22, 67, 43),
nrow = 3, byrow = TRUE)
# Calling the print() function
# using na.print parameter
print(x, na.print = "")
输出
[, 1] [, 2] [, 3]
[1, ] 2 5
[2, ] 8
[3, ] 22 67 43