在 R 编程中命名矩阵的行和列 - rownames() 和 colnames()函数
R 语言中的rownames()
函数用于将名称设置为矩阵的行。
Syntax:
rownames(x) <- value
Parameters:
x: Matrix
value: Vector of names to be set
例子:
# R program to provide a name
# to rows of a Matrix
# Creating a 3X3 Matrix
A = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), 3, 3, byrow = TRUE)
# Calling rownames() Function
rownames(A) <- letters[1:3]
print(A)
输出:
[, 1] [, 2] [, 3]
a 1 2 3
b 4 5 6
c 7 8 9
colnames()函数
R 语言中的colnames()
函数用于将名称设置为矩阵的列。
Syntax:
colnames(x) <- value
Parameters:
x: Matrix
value: Vector of names to be set
例子:
# R program to provide a name
# to columns of a Matrix
# Creating a 3X3 Matrix
A = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), 3, 3, byrow = TRUE)
# Calling colnames() Function
colnames(A) <- letters[1:3]
print(A)
输出:
a b c
[1, ] 1 2 3
[2, ] 4 5 6
[3, ] 7 8 9