第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

下標(biāo)超出范圍-一般定義和解決方案?

下標(biāo)超出范圍-一般定義和解決方案?

千巷貓影 2019-10-16 10:51:01
使用RI時(shí),經(jīng)常出現(xiàn)錯(cuò)誤消息“下標(biāo)超出范圍”。例如:# 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這將產(chǎn)生以下錯(cuò)誤Error in reach_mat[i, alter] = 1 : subscript out of bounds。但是,我的問題不是關(guān)于這段特定的代碼(即使對(duì)解決這一問題也有幫助),但是我的問題更籠統(tǒng):下標(biāo)越界錯(cuò)誤的定義是什么?是什么原因造成的?有沒有通用的方法可以解決這種錯(cuò)誤?
查看完整描述

3 回答

?
森欄

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊

這是因?yàn)槟鷩L試訪問數(shù)組之外的數(shù)組。


我將向您展示如何調(diào)試此類錯(cuò)誤。


我設(shè)置 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()以查看當(dāng)前的變量


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

     "i"               "j"                     "m"              

    "reach_mat"       "this_node_reach"

現(xiàn)在,我將看到變量的尺寸:


Browse[1]> i

[1] 1

Browse[1]> j

[1] 21

Browse[1]> alter

[1] 22

Browse[1]> dim(reach_mat)

[1] 21 21

您會(huì)看到alter已超出范圍。22> 21。在行中:


  reach_mat[i, alter] = 1

為避免此類錯(cuò)誤,我個(gè)人這樣做:


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

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

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

編輯矢量化解決方案


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


您可以替換:


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


查看完整回答
反對(duì) 回復(fù) 2019-10-16
?
尚方寶劍之說

TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個(gè)贊

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


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) 

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



查看完整回答
反對(duì) 回復(fù) 2019-10-16
?
撒科打諢

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊

我有時(shí)會(huì)遇到相同的問題。我只能回答你的第二個(gè)要點(diǎn),因?yàn)槲也幌衿渌Z言那樣熟練地使用R。我發(fā)現(xiàn)標(biāo)準(zhǔn)for循環(huán)有一些意外的結(jié)果。說x = 0


for (i in 1:x) {

  print(i)

}

輸出是


[1] 1

[1] 0

以python為例


for i in range(x):

  print i

什么也沒做。沒有進(jìn)入循環(huán)。


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


查看完整回答
反對(duì) 回復(fù) 2019-10-16
  • 3 回答
  • 0 關(guān)注
  • 2335 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)