Often you may want to remove the column names from a matrix in R.

For example, a matrix may have the column names **A**, **B** and **C** and you would instead like them to simply be **[,1]** , **[,2]** and **[,3]** which are simply the default column positions.

Here are the most common ways to do so:

**Method 1: Remove Column Names Using unname()**

#remove column names from matrix my_matrix <- unname(my_matrix)

This method uses the **unname()** function to remove all column names from a matrix in R.

Note that we use the **<-** operator to assign the results of the **unname()** function back to the original matrix.

**Method 2: Remove Column Names Using colnames()**

#remove column names from matrixcolnames(my_matrix) <- NULL

This method uses the **colnames()** function to remove all column names from a matrix in R by assigning the value of the column names to be **NULL**, which effectively removes all existing column names.

The following examples show how to use both of these methods in practice.

**Note**: The **unname()** and **colnames()** functions both come built-in with base R. This means you don’t have to load or install any external packages to use both of these functions.

**Example 1: How to Remove Column Names Using unname()**

Suppose we create the following matrix in R named **my_matrix** that contains six columns and three rows:

#create matrix my_matrix <- matrix(1:18, ncol=6) #add column names to matrix colnames(my_matrix) <- c('A', 'B', 'C', 'D', 'E', 'F') #view matrix my_matrix A B C D E F [1,] 1 4 7 10 13 16 [2,] 2 5 8 11 14 17 [3,] 3 6 9 12 15 18

Note that we used the **colnames()** function to specify the following column names for the matrix:

**A, B, C, D, E, F**

Suppose that we would like to remove each of these column names from the matrix.

One way to do so is by using the **unname()** function as follows:

#remove column names from matrix my_matrix <- unname(my_matrix) #view updated matrix my_matrix [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 4 7 10 13 16 [2,] 2 5 8 11 14 17 [3,] 3 6 9 12 15 18

Notice that each of the column names have been removed from the matrix and the new column names are simply the index positions of the columns.

**Example 2: How to Remove Column Names Using colnames()**

Once again, suppose we create the following matrix in R named **my_matrix** that contains six columns and three rows:

#create matrix my_matrix <- matrix(1:18, ncol=6) #add column names to matrix colnames(my_matrix) <- c('A', 'B', 'C', 'D', 'E', 'F') #view matrix my_matrix A B C D E F [1,] 1 4 7 10 13 16 [2,] 2 5 8 11 14 17 [3,] 3 6 9 12 15 18

Once again, we used the **colnames()** function to specify column names for the matrix.

Suppose that we would like to remove each of these column names from the matrix.

One way to do so is by using the **colnames()** function to assign each of the column names to be equal to **NULL**:

#remove column names from matrix colnames(my_matrix) <- NULL #view updated matrix my_matrix [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 4 7 10 13 16 [2,] 2 5 8 11 14 17 [3,] 3 6 9 12 15 18

Notice that each of the column names have been removed from the matrix and the new column names are simply the index positions of the columns.

**Additional Resources**

The following tutorials explain how to perform other common tasks in R:

How to Add Row to Data Frame Using dplyr

How to Arrange Rows in Custom Order Using dplyr

How to Filter Based on Factor in dplyr

How to Use top_n() in dplyr