There is an error of subscript out of bounds:

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

# Calculate reachability for each vertix

reachability <- function(f, n) {

reach_mat = matrix(nrow = vcount(f),

ncol = vcount(f))

for (i in 1:vcount(f)) {

reach_mat[i,] = 0

this_node_reach <- subcomponent(f, (i - 1), mode = n)

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

This is showing an error Error in reach_mat[i, alter] = 1 : subscript out of bounds

So my question is:

Can someone define me what is subscript-out-of-bounds error and what's the cause behind it?

How to sort out this type of error in a generic way?