Naming Rows and Columns of a Matrix in R Programming – rownames() and colnames() Function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

      a    b    c
[1, ] 1    2    3
[2, ] 4    5    6
[3, ] 7    8    9



My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.