Naming Rows and Columns of a Matrix in R Programming – rownames() and colnames() Function
Last Updated :
05 Jun, 2020
rownames()
function in R Language is used to set the names to rows of a matrix.
Syntax:
rownames(x) <- value
Parameters:
x: Matrix
value: Vector of names to be set
Example:
A = matrix(c( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ), 3 , 3 , byrow = TRUE)
rownames(A) < - letters[ 1 : 3 ]
print (A)
|
Output:
[, 1] [, 2] [, 3]
a 1 2 3
b 4 5 6
c 7 8 9
colnames() Function
colnames()
function in R Language is used to set the names to columns of a matrix.
Syntax:
colnames(x) <- value
Parameters:
x: Matrix
value: Vector of names to be set
Example:
A = matrix(c( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ), 3 , 3 , byrow = TRUE)
colnames(A) < - letters[ 1 : 3 ]
print (A)
|
Output:
a b c
[1, ] 1 2 3
[2, ] 4 5 6
[3, ] 7 8 9
Share your thoughts in the comments
Please Login to comment...