How to Delete Column & Row Names of Matrix in R (2 Examples)

In this article you’ll learn how to drop all row and column names of a matrix object in the R programming language.

Example Data

data(iris)                        # Load & modify example data
iris_mat <- as.matrix(iris[ , 1:4])
rownames(iris_mat) <- 1:nrow(iris_mat)
head(iris_mat)                    # Display head of example data
#   Sepal.Length Sepal.Width Petal.Length Petal.Width
# 1          5.1         3.5          1.4         0.2
# 2          4.9         3.0          1.4         0.2
# 3          4.7         3.2          1.3         0.2
# 4          4.6         3.1          1.5         0.2
# 5          5.0         3.6          1.4         0.2
# 6          5.4         3.9          1.7         0.4

Example 1: Removing Matrix Column Names

colnames(iris_mat) <- NULL        # Apply colnames() function
head(iris_mat)                    # Displaying updated iris matrix
#   [,1] [,2] [,3] [,4]
# 1  5.1  3.5  1.4  0.2
# 2  4.9  3.0  1.4  0.2
# 3  4.7  3.2  1.3  0.2
# 4  4.6  3.1  1.5  0.2
# 5  5.0  3.6  1.4  0.2
# 6  5.4  3.9  1.7  0.4

Example 2: Removing Matrix Row Names

rownames(iris_mat) <- NULL        # Apply rownames() function
head(iris_mat)                    # Displaying updated iris matrix
#      [,1] [,2] [,3] [,4]
# [1,]  5.1  3.5  1.4  0.2
# [2,]  4.9  3.0  1.4  0.2
# [3,]  4.7  3.2  1.3  0.2
# [4,]  4.6  3.1  1.5  0.2
# [5,]  5.0  3.6  1.4  0.2
# [6,]  5.4  3.9  1.7  0.4

Related Articles

In the following, you can find some further resources on topics such as RStudio, extracting data, and matrices:

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top