亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

下標超出范圍-一般定義和解決方案?

下標超出范圍-一般定義和解決方案?

千巷貓影 2019-10-16 10:51:01
使用RI時,經常出現錯誤消息“下標超出范圍”。例如:# Load necessary libraries and datalibrary(igraph)library(NetData)data(kracknets, package = "NetData")# Reduce dataset to nonzero edgeskrack_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 attributesfor (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 vertixreachability <- 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。但是,我的問題不是關于這段特定的代碼(即使對解決這一問題也有幫助),但是我的問題更籠統:下標越界錯誤的定義是什么?是什么原因造成的?有沒有通用的方法可以解決這種錯誤?
查看完整描述

3 回答

?
森欄

TA貢獻1810條經驗 獲得超5個贊

這是因為您嘗試訪問數組之外的數組。


我將向您展示如何調試此類錯誤。


我設置 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 bounds

Enter a frame number, or 0 to exit   

1: reachability(krack_full, "in")

輸入1,我得到


 Called from: top level 

我鍵入ls()以查看當前的變量


  1] "*tmp*"           "alter"           "g"               

     "i"               "j"                     "m"              

    "reach_mat"       "this_node_reach"

現在,我將看到變量的尺寸:


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。在行中:


  reach_mat[i, alter] = 1

為避免此類錯誤,我個人這樣做:


嘗試使用applyxx功能。他們比for

我使用seq_along而不是1:n(1:0]

如果可以避免mat[i,j]索引訪問,請嘗試考慮矢量化解決方案。

編輯矢量化解決方案


例如,在這里我看到您沒有使用set.vertex.attribute向量化的事實。


您可以替換:


# 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])


查看完整回答
反對 回復 2019-10-16
?
尚方寶劍之說

TA貢獻1788條經驗 獲得超4個贊

如果這對任何人有幫助,我在將purr :: map()與我編寫的函數結合使用時會遇到以下問題:


find_nearby_shops <- function(base_account) {

   states_table %>% 

        filter(state == base_account$state) %>% 

        left_join(target_locations, by = c('border_states' = 'state')) %>% 

        mutate(x_latitude = base_account$latitude,

               x_longitude = base_account$longitude) %>% 

        mutate(dist_miles = geosphere::distHaversine(p1 = cbind(longitude, latitude), 

                                                     p2 = cbind(x_longitude, x_latitude))/1609.344)

}


nearby_shop_numbers <- base_locations %>% 

    split(f = base_locations$id) %>% 

    purrr::map_df(find_nearby_shops) 

有時我會在樣本中得到這個錯誤,但是大多數時候我不會。問題的根源是base_locations表(PR)中的某些狀態不存在于states_table中,因此本質上我已經過濾掉了所有內容,并將一個空表傳遞給mutate。 這個故事的寓意是,您可能遇到數據問題,而沒有(僅僅是)代碼問題(因此您可能需要清理數據)。



查看完整回答
反對 回復 2019-10-16
?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

我有時會遇到相同的問題。我只能回答你的第二個要點,因為我不像其他語言那樣熟練地使用R。我發現標準for循環有一些意外的結果。說x = 0


for (i in 1:x) {

  print(i)

}

輸出是


[1] 1

[1] 0

以python為例


for i in range(x):

  print i

什么也沒做。沒有進入循環。


我希望如果x = 0在R中不輸入該循環。但是,1:0是數字的有效范圍。除了有一個if包裝for循環的語句外,我還沒有找到一個好的解決方法


查看完整回答
反對 回復 2019-10-16
  • 3 回答
  • 0 關注
  • 2323 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號