如何修复:R 中的下标越界
下标越界:这是在 R 中可能遇到的最常见错误之一,具有以下形式:
Error in y[,6]: subscript out of bounds
原因:当程序员试图访问不存在的行或列时,编译器会产生此错误。
创建矩阵:
让我们首先创建一个矩阵。例如,我们创建了一个 5 行 3 列的矩阵垫。它的值是使用 sample.int()函数初始化的。此函数用于从数据集中获取随机元素。
R
# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
#print matrix
print(mat)
R
# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
# Try to print the 6th row of matrix
mat[6, ]
R
# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
# Print the number of rows in matrix
nrow(mat)
R
# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
# Try to print the 5th row of the matrix
mat[5,]
R
# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
# Try to print the 4th column of the matrix
mat[, 4]
R
# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
# Print the number of columns in the matrix
ncol(mat)
R
# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
# Try to print the 3th column of the matrix
mat[, 3]
R
# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
# Try to print the 6th row and the 4th column
# of the matrix
mat[6, 4]
R
# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
# Print the number of rows and columns in the matrix
dim(mat)
R
# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
# Print the number of rows and columns
# in the matrix
mat[5,3]
输出:
示例 1:下标越界(行):
下面的代码试图访问不存在的第 6 行。
R
# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
# Try to print the 6th row of matrix
mat[6, ]
输出:
矩阵的第 6 行不存在,因此我们得到下标越界错误。请注意,我们始终可以使用 nrow()函数来检查矩阵中有多少行:
例子:
R
# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
# Print the number of rows in matrix
nrow(mat)
输出:
矩阵中只有 5 行。因此,我们可以访问小于或等于 5 的列。现在让我们尝试访问第 3 列。
R
# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
# Try to print the 5th row of the matrix
mat[5,]
输出:
示例 2:下标越界(在列中)。
下面的代码试图访问不存在的第四列。
R
# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
# Try to print the 4th column of the matrix
mat[, 4]
输出:
矩阵的第 4 列不存在,因此我们得到下标越界错误。请注意,我们始终可以使用 ncol()函数来检查矩阵中有多少列。
例子:
R
# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
# Print the number of columns in the matrix
ncol(mat)
输出:
矩阵中只有 3 列。因此,我们可以访问小于或等于 3 的列。现在让我们尝试访问第 3 列。
例子:
R
# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
# Try to print the 3th column of the matrix
mat[, 3]
输出:
示例 3:下标越界(行和列):
下面的代码试图访问第 6 行和第 4 列。
R
# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
# Try to print the 6th row and the 4th column
# of the matrix
mat[6, 4]
输出:
矩阵的第 6 行和第 4 列不存在,因此我们得到下标越界错误。请注意,我们总是可以使用 dim()函数来检查矩阵中有多少行和列。
R
# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
# Print the number of rows and columns in the matrix
dim(mat)
输出:
矩阵中只有 5 行 3 列。因此,我们可以访问小于等于 5 的行和小于等于 3 的列。现在让我们尝试访问存储在第 5 行和第 3 列的值。
R
# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
# Print the number of rows and columns
# in the matrix
mat[5,3]
输出: