Remove Rows/Columns in R Arrays
To remove specific rows or columns from an array, you can use subset operators [] and logical operators in the R language. Here are some examples of code:
- Delete specific rows:
# 创建一个示例数组
data <- matrix(1:12, nrow=4)
# 删除第2行
data <- data[-2,]
print(data)
- Delete specific column:
# 创建一个示例数组
data <- matrix(1:12, nrow=4)
# 删除第2列
data <- data[,-2]
print(data)
In the example above, we used a negative sign to specify the index of the row or column to be deleted. By combining the negative sign with the index of the row or column to be deleted, you can remove specific rows or columns from the array.