Adding and Deleting Rows and Columns in a Matrix with R

Adding and Deleting Rows and Columns in a Matrix

Adding Rows

To add rows to a matrix, you use the rbind() function (row-bind), which stacks matrices or vectors by rows.

Example 1: Adding a Row to a Matrix

Suppose you have a matrix and want to add a new row: 

# Example 1: Adding a row to a matrix
m <- matrix(1:6, nrow = 2)
print(m)
# New row to add
new_row <- c(7, 8, 9)
# Add the row
m_added_row <- rbind(m, new_row)
print(m_added_row)
# The result is:
# Original matrix
#       [,1] [,2] [,3]
# [1,]    1    3    5
# [2,]    2    4    6
# Matrix after adding the row
#      [,1] [,2] [,3]
# [1,]    1    3    5
# [2,]    2    4    6
# [3,]    7    8    9

Adding Columns

To add columns, you use the cbind() function (column-bind), which stacks matrices or vectors by columns.

Example 2: Adding a Column to a Matrix

Suppose you want to add a new column to a matrix: 

# Example 2: Adding a column to a matrix
m <- matrix(1:6, nrow = 2)
print(m)
# New column to add
new_column <- c(7, 8)
# Add the column
m_added_col <- cbind(m, new_column)
print(m_added_col)
# The result is:
# Original matrix
#       [,1] [,2] [,3]
# [1,]    1    3    5
# [2,]    2    4    6
# Matrix after adding the column
#       [,1] [,2] [,3] [,4]
# [1,]    1    3    5    7
# [2,]    2    4    6    8

Deleting Rows

To delete rows from a matrix, you can use negative indexing.

Example 3: Deleting a Row from a Matrix

Suppose you want to delete the second row: 

# Example 3: Deleting a row from a matrix
m <- matrix(1:6, nrow = 2)
print(m)
# Delete the second row
m_without_row <- m[-2, ]
print(m_without_row)
# The result is:
# Original matrix
#      [,1] [,2] [,3]
# [1,]    1    3    5
# [2,]    2    4    6
# Matrix after deleting the row
#      [,1] [,2] [,3]
# [1,]    1    3    5

Deleting Columns

To delete columns from a matrix, you also use negative indexing.

Example 4: Deleting a Column from a Matrix

Suppose you want to delete the third column: 

# Example 4: Deleting a column from a matrix
m <- matrix(1:6, nrow = 2)
print(m)
# Delete the third column
m_without_col <- m[, -3]
print(m_without_col)
# The result is:
# Original matrix
#       [,1] [,2] [,3]
# [1,]    1    3    5
# [2,]    2    4    6
# Matrix after deleting the column
#       [,1] [,2]
# [1,]    1    3
# [2,]    2    4

 Adding Rows or Columns with Names

You can also add rows or columns with row and column names.

Example 5: Adding a Row with Names 

# Example 5: Adding a row with names
m <- matrix(1:6, nrow = 2)
rownames(m) <- c("Row1", "Row2")
colnames(m) <- c("Col1", "Col2", "Col3")
print(m)
# New row with name
new_row <- c(7, 8, 9)
rownames_new_row <- "Row3"
# Add the row with name
m_with_named_row <- rbind(m, Row3 = new_row)
print(m_with_named_row)
# The result is:
# Original matrix with names
#        Col1 Col2 Col3
# Row1      1    3    5
# Row2      2    4    6
# Matrix after adding the row with name
#         Col1 Col2 Col3
# Row1      1    3    5
# Row2      2    4    6
# Row3      7    8    9

Using rbind() and cbind() with Matrices of Different Sizes

When adding rows or columns, make sure the dimensions are compatible. If dimensions do not match, rbind() and cbind() will generate an error.

Example 6: Adding Columns with Compatible Dimensions 

# Example 6: Adding columns with compatible dimensions
m1 <- matrix(1:6, nrow = 2)
m2 <- matrix(7:8, nrow = 2)
# Add the columns
m_combined <- cbind(m1, m2)
print(m_combined)
# The result is:
# Combined matrix
#      [,1] [,2] [,3] [,4]
# [1,]    1    3    7    8
# [2,]    2    4    9   10

 Summary of Adding and Deleting Rows and Columns

  • Adding Rows: Use rbind() to add rows to a matrix.
  • Adding Columns: Use cbind() to add columns to a matrix.
  • Deleting Rows: Use negative indexing to remove rows.
  • Deleting Columns: Use negative indexing to remove columns.
  • Adding Rows/Columns with Names: Use rbind() and cbind() while specifying row and column names.
  • Ensuring Compatibility: Ensure dimensions match when using rbind() and cbind().

These methods enable you to efficiently modify the dimensions of a matrix by adding or deleting rows and columns according to your data manipulation needs.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *