How to Remove Columns from a Matrix in R


Often you may want to remove one or more specific columns from a matrix in R.

Fortunately this is straightforward to do and you can use the following methods to do so:

Method 1: Remove One Column from Matrix by Position

#remove column 3 from matrix
my_matrix[, -3]

Method 2: Remove Multiple Columns from Matrix by Position

#remove columns 3 and 5 from matrix
my_matrix[, c(-3, -5)]

Method 3: Remove One Column from Matrix by Name

#remove column with column name 'A' from matrix
my_matrix[, colnames(my_matrix) != 'A']

Method 4: Remove Multiple Columns from Matrix by Name

#remove columns with column names 'A' and 'C' from matrix
my_matrix[, !colnames(my_matrix) %in% c('A', 'C')]

The following examples show how to use each of these methods in practice with the following matrix in R named my_matrix:

#create matrix
my_matrix <- matrix(1:20, ncol=5)

#specify column names for matrix
colnames(my_matrix) <- c('A', 'B', 'C', 'D', 'E')
#view matrix
my_matrix

     A B  C  D  E
[1,] 1 5  9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20

Example 1: Remove One Column from Matrix by Position

We can use the following syntax to only remove the column in position 3 of the matrix:

#remove column 3 from matrix
my_matrix[, -3]

     A B  D  E
[1,] 1 5 13 17
[2,] 2 6 14 18
[3,] 3 7 15 19
[4,] 4 8 16 20

This returns the entire matrix with the column in position 3 (column “C”) removed.

By using a negative sign in front of the 3 we specify that we would like to return all columns except the column in position 3.

Example 2: Remove Multiple Columns from Matrix by Position

We can use the following syntax to only remove the column in position 3 of the matrix:

#remove columns 3 and 5 from matrix
my_matrix[, c(-3, -5)]

     A B  D
[1,] 1 5 13
[2,] 2 6 14
[3,] 3 7 15
[4,] 4 8 16

This returns the entire matrix with the columns in positions 3 (column “C”) and 5 (column “E”) removed.

Note that you can use similar notation to remove as many columns based on position as you would like.

Example 3: Remove One Column from Matrix by Name

We can use the following syntax to only remove the column with a column name A from the matrix:

#remove column with column name 'A' from matrix
my_matrix[, colnames(my_matrix) != 'A']

     B  C  D  E
[1,] 5  9 13 17
[2,] 6 10 14 18
[3,] 7 11 15 19
[4,] 8 12 16 20

This returns the entire matrix with the column named “A” removed from the matrix.

Note that the operator != stands for “not equal” so we effectively return all columns from the matrix that have a name not equal to A.

Example 4: Remove Multiple Columns from Matrix by Name

We can use the following syntax to remove the columns with column names A and C from the matrix:

#remove columns with column names 'A' and 'C' from matrix
my_matrix[, !colnames(my_matrix) %in% c('A', 'C')]

     B  D  E
[1,] 5 13 17
[2,] 6 14 18
[3,] 7 15 19
[4,] 8 16 20

This returns the entire matrix with the columns named “A” and “C” removed from the matrix.

You can also include as many column names as you would like in the vector following the %in% operator to remove even more columns by name.

Additional Resources

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

How to Sort a Matrix in R
How to Remove NA from Matrix in R
How to Convert Data Frame to Matrix in R
How to Convert a Table to a Matrix in R

Leave a Reply

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