下标超出范围-一般定义和解决方案?

使用RI时,经常出现错误消息“下标超出范围”。例如:


# 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。


但是,我的问题不是关于这段特定的代码(即使对解决这一问题也有帮助),但是我的问题更笼统:


下标越界错误的定义是什么?是什么原因造成的?

有没有通用的方法可以解决这种错误?


千巷猫影
浏览 1998回答 3
3回答

森栏

这是因为您尝试访问数组之外的数组。我将向您展示如何调试此类错误。我设置 options(error=recover)我跑reach_full_in <- reachability(krack_full, 'in') 我得到:reach_full_in <- reachability(krack_full, 'in')Error in reach_mat[i, alter] = 1 : subscript out of boundsEnter a frame number, or 0 to exit&nbsp; &nbsp;1: reachability(krack_full, "in")输入1,我得到&nbsp;Called from: top level&nbsp;我键入ls()以查看当前的变量&nbsp; 1] "*tmp*"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"alter"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"g"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;"i"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"j"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"m"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; "reach_mat"&nbsp; &nbsp; &nbsp; &nbsp;"this_node_reach"现在,我将看到变量的尺寸:Browse[1]> i[1] 1Browse[1]> j[1] 21Browse[1]> alter[1] 22Browse[1]> dim(reach_mat)[1] 21 21您会看到alter已超出范围。22> 21。在行中:&nbsp; reach_mat[i, alter] = 1为避免此类错误,我个人这样做:尝试使用applyxx功能。他们比for我使用seq_along而不是1:n(1:0]如果可以避免mat[i,j]索引访问,请尝试考虑矢量化解决方案。编辑矢量化解决方案例如,在这里我看到您没有使用set.vertex.attribute向量化的事实。您可以替换:# Set vertex attributesfor (i in V(krack_full)) {&nbsp; &nbsp; for (j in names(attributes)) {&nbsp; &nbsp; &nbsp; &nbsp; krack_full <- set.vertex.attribute(krack_full, j, index=i, attributes[i+1,j])&nbsp; &nbsp; }}这样:##&nbsp; set.vertex.attribute is vectorized!##&nbsp; no need to loop over vertex!for (attr in names(attributes))&nbsp; &nbsp; &nbsp; krack_full <<- set.vertex.attribute(krack_full,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;attr, value = attributes[,attr])

尚方宝剑之说

如果这对任何人有帮助,我在将purr :: map()与我编写的函数结合使用时会遇到以下问题:find_nearby_shops <- function(base_account) {&nbsp; &nbsp;states_table %>%&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; filter(state == base_account$state) %>%&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; left_join(target_locations, by = c('border_states' = 'state')) %>%&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; mutate(x_latitude = base_account$latitude,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x_longitude = base_account$longitude) %>%&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; mutate(dist_miles = geosphere::distHaversine(p1 = cbind(longitude, latitude),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p2 = cbind(x_longitude, x_latitude))/1609.344)}nearby_shop_numbers <- base_locations %>%&nbsp;&nbsp; &nbsp; split(f = base_locations$id) %>%&nbsp;&nbsp; &nbsp; purrr::map_df(find_nearby_shops)&nbsp;有时我会在样本中得到这个错误,但是大多数时候我不会。问题的根源是base_locations表(PR)中的某些状态不存在于states_table中,因此本质上我已经过滤掉了所有内容,并将一个空表传递给mutate。 这个故事的寓意是,您可能遇到数据问题,而没有(仅仅是)代码问题(因此您可能需要清理数据)。

撒科打诨

我有时会遇到相同的问题。我只能回答你的第二个要点,因为我不像其他语言那样熟练地使用R。我发现标准for循环有一些意外的结果。说x = 0for (i in 1:x) {&nbsp; print(i)}输出是[1] 1[1] 0以python为例for i in range(x):&nbsp; print i什么也没做。没有进入循环。我希望如果x = 0在R中不输入该循环。但是,1:0是数字的有效范围。除了有一个if包装for循环的语句外,我还没有找到一个好的解决方法
打开App,查看更多内容
随时随地看视频慕课网APP