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

function in R Language is used to set the names to rows of a matrix.**rownames()**

Syntax:

rownames(x) <- value

Parameters:x:Matrixvalue:Vector of names to be set

**Example:**

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

**Output:**

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

#### colnames() Function

function in R Language is used to set the names to columns of a matrix.**colnames()**

Syntax:

colnames(x) <- value

Parameters:x:Matrixvalue:Vector of names to be set

**Example:**

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

**Output:**

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.