下标超出范围-一般定义和解决方案? [英] Subscript out of bounds - general definition and solution?
问题描述
使用R时,我经常收到错误消息下标超出范围". 例如:
When working with R I frequently get the error message "subscript out of bounds". For example:
# Load necessary libraries and data
library(igraph)
library(NetData)
data(kracknets, package = "NetData")
# Reduce dataset to nonzero edges
krack_full_nonzero_edges <- subset(krack_full_data_frame, (advice_tie > 0 | friendship_tie > 0 | reports_to_tie > 0))
# convert to graph data farme
krack_full <- graph.data.frame(krack_full_nonzero_edges)
# Set vertex attributes
for (i in V(krack_full)) {
for (j in names(attributes)) {
krack_full <- set.vertex.attribute(krack_full, j, index=i, attributes[i+1,j])
}
}
# Calculate reachability for each vertix
reachability <- function(g, m) {
reach_mat = matrix(nrow = vcount(g),
ncol = vcount(g))
for (i in 1:vcount(g)) {
reach_mat[i,] = 0
this_node_reach <- subcomponent(g, (i - 1), mode = m)
for (j in 1:(length(this_node_reach))) {
alter = this_node_reach[j] + 1
reach_mat[i, alter] = 1
}
}
return(reach_mat)
}
reach_full_in <- reachability(krack_full, 'in')
reach_full_in
这会产生以下错误Error in reach_mat[i, alter] = 1 : subscript out of bounds
.
但是,我的问题不是关于这段特定的代码(尽管也可以解决该问题),但是我的问题更笼统:
However, my question is not about this particular piece of code (even though it would be helpful to solve that too), but my question is more general:
- 下标越界错误的定义是什么?是什么原因造成的?
- 有没有通用的方法来处理这种错误?
推荐答案
这是因为您尝试访问超出其边界的数组.
This is because you try to access an array out of its boundary.
我将向您展示如何调试此类错误.
I will show you how you can debug such errors.
- 我设置了
options(error=recover)
-
我运行
reach_full_in <- reachability(krack_full, 'in')
我得到了:
- I set
options(error=recover)
I run
reach_full_in <- reachability(krack_full, 'in')
I get :
reach_full_in <- reachability(krack_full, 'in')
Error in reach_mat[i, alter] = 1 : subscript out of bounds
Enter a frame number, or 0 to exit
1: reachability(krack_full, "in")
我输入1并得到
I enter 1 and I get
Called from: top level
我键入ls()
来查看我当前的变量
I type ls()
to see my current variables
1] "*tmp*" "alter" "g"
"i" "j" "m"
"reach_mat" "this_node_reach"
现在,我将看到变量的尺寸:
Now, I will see the dimensions of my variables :
Browse[1]> i
[1] 1
Browse[1]> j
[1] 21
Browse[1]> alter
[1] 22
Browse[1]> dim(reach_mat)
[1] 21 21
您会看到alter已超出范围. 22> 21.在该行中:
You see that alter is out of bounds. 22 > 21 . in the line :
reach_mat[i, alter] = 1
为避免此类错误,我个人是这样做的:
To avoid such error, personally I do this :
- 尝试使用
applyxx
功能.它们比for
更安全
- 我使用
seq_along
而不是1:n
(1:0) - 如果可以避免使用
mat[i,j]
索引访问,请尝试考虑矢量化解决方案.
- Try to use
applyxx
function. They are safer thanfor
- I use
seq_along
and not1:n
(1:0) - Try to think in a vectorized solution if you can to avoid
mat[i,j]
index access.
EDIT矢量化解决方案
例如,在这里我看到您没有使用set.vertex.attribute
是矢量化的事实.
For example, here I see that you don't use the fact that set.vertex.attribute
is vectorized.
您可以替换:
# Set vertex attributes
for (i in V(krack_full)) {
for (j in names(attributes)) {
krack_full <- set.vertex.attribute(krack_full, j, index=i, attributes[i+1,j])
}
}
由此:
## set.vertex.attribute is vectorized!
## no need to loop over vertex!
for (attr in names(attributes))
krack_full <<- set.vertex.attribute(krack_full,
attr, value = attributes[,attr])
这篇关于下标超出范围-一般定义和解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!