rownames() and colnames() functions in R

rownames() and colnames() functions in R

If you create an object like a matrix but forget to name its rows and columns, the rownames() and colnames() functions in R will help you. In this article, we will give you instructions to use the rownames() and colnames() functions to name rows and columns of a matrix-like object. Now, let’s move on.

What are the rownames() and colnames() functions in R?

The rownames() function

The rownames() function gives names for rows to an object like a matrix or a group of vectors.

Syntax: 

rownames(object, do.NULL, prefix)

rownames(object) = name

Parameters: 

  • object: The object that needs to be named rows.
  • do.NULL: The initial name of rows. The default value is TRUE.
  • prefix: Prefix the name of the rows if the do.NULL is FALSE.
  • name: The vector or list or array of names.

The colnames() function

The colnames() function gives names for columns to an object like a matrix or a group of vectors.

Syntax: 

colnames(object, do.NULL, prefix)

colnames(object) = name

Parameters: 

  • object: The object that needs to be named columns.
  • do.NULL: The initial name of columns. The default value is TRUE.
  • prefix: Prefix the name of the columns if the do.NULL is FALSE.
  • name: The vector or list or array of names.

How to use the rownames() and colnames() functions?

Name the rows and columns of a matrix

To warm up, we will learn how to name rows and columns of a matrix. In the example below, we will create two vectors to store elements of the matrix and create a matrix having a size 4×4 from the vectors. Then, we use the rownames() and colnames() functions to name the matrix’s rows and columns.

Code:

# Create two vectors containing values for the matrix 
vector1 = c(4, 5, 7, 2, 8, 9, 12, 22)
vector2 = c(1, 0, 11, 6, 3, 15, 21, 13)

# Create a 4x4 matrix from two vectors
matrix = matrix(c(vector1, vector2), nrow=4, ncol = 4)

# Name rows of the matrix
rownames(matrix) = c("row1", "row2", "row3", "row4")

# Name columns of the matrix
colnames(matrix) = c("col1", "col2", "col3", "col4")

cat("The matrix after naming rows and columns:\n")
print(matrix)

Result:

The matrix after naming rows and columns:
     col1 col2 col3 col4
row1    4    8    1    3
row2    5    9    0   15
row3    7   12   11   21
row4    2   22    6   13

Name the rows and columns of a data frame

The rownames() and colnames() functions can be used to name or rename rows and columns of a data frame. Look at the following example to see more details.

Code:

# Create vectors containing values for the data frame 
vector1 = c("John", "David", "Mary", "Thomas", "Susan", "Emily")
vector2 = c(22, 17, 41, 35, 18, 38)
vector3 = c("Male", "Male", "Female", "Male", "Female", "Female")

# Create a data frame from three vectors
df = data.frame(vector1, vector2, vector3)

# Name rows of the data frame
rownames(df) = c("row1", "row2", "row3", "row4", "row5", "row6")

# Name columns of the data frame
colnames(df) = c("Name", "Age", "Gender")

cat("The data frame after naming rows and columns:\n")
print(df)

Result:

The data frame after naming rows and columns:
       Name Age Gender
row1   John  22   Male
row2  David  17   Male
row3   Mary  41 Female
row4 Thomas  35   Male
row5  Susan  18 Female
row6  Emily  38 Female

Summary

In summary, the rownames() and colnames() functions set names for rows and columns of objects such as matrices, data frames, etc. If the objects’ rows and columns have the initial names, we can use the function to rename them.

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *