Subscript poza granicami - ogólna definicja i rozwiązanie?

Podczas pracy z R często otrzymuję komunikat o błędzie „indeks poza zakresem”.Na przykład:

# 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

Powoduje to następujący błądError in reach_mat[i, alter] = 1 : subscript out of bounds.

Moje pytanie nie dotyczy jednak tego konkretnego fragmentu kodu (choć pomocne byłoby również rozwiązanie tego problemu), ale moje pytanie jest bardziej ogólne:

Jaka jest definicja błędu subscript-out-of-bounds? Co to powoduje?Czy są jakieś ogólne sposoby podejścia do tego rodzaju błędów?

questionAnswers(6)

yourAnswerToTheQuestion