in Education by
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? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
Your code is throwing the error because you are accessing an array that is out of its boundary. The following are the steps to debug such errors. First set the options(error=recover) Then run reach_full_in <- reachability(krack_full, 'in'). You will get : 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") If you enter 1, then will get: Called from: top level After that, type ls() to see the current variables: 1] "*tmp*" "alter" "g" "i" "j" "m" "reach_mat" "this_node_reach" Now, you will able to observe the dimension of the variables: Browse[1]> i [1] 1 Browse[1]> j [1] 21 Browse[1]> alter [1] 22 Browse[1]> dim(reach_mat) [1] 21 21 You see that alter is out of bounds. 22 > 21 . in the line : reach_mat[i, alter] = 1 To handle such errors in R programming, I would recommend the following steps: Try to use applyxx function. They are safer than for loop. Use seq_along and not 1:n (1:0). Try to avoid mat[i,j] index access.

Related questions

0 votes
    How can I check for missing packages and call for installed.packages() to install them smartly? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    Can someone tell me the difference between require() and library() in R? Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    I want a way to create a empty data.frame in R, I just want to point out data types for each column and name it without any row created. df...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I have file called q.r and it has chmod of 755, how can I run it using a command-line? sayHello...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I work on Mac OSX and want to install a RJSONIO package in my R, I am a beginner currently and I have no ... someone explain it to me? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How can I merge and combine two values in R? For instance: tmp = cbind("QWE", "RT") tmp # [,1] [,2] # ... ,RT" How can I perform this? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    Can someone tell me a function which can return a specified no. of rows picked randomly without any replacement from a DataFrame in R? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I am ploating simple linear regression plot in R and I want to save it as JPEG or PNG file, How can I do ... it is, Is it possible? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    How to write a trycatch code to sort out an error in web downloading. url...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I want to supply some command-line parameters from a R script I have. And it runs on windows. I just want it ... help me with it? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    Is there any shortcut method to find an index of an element in a vector, I have an element Q with me ... someone answer this doubt? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    What does startup profiles of R professionals include, I have no customization in R and I want to add some. ... highlight my syntax. Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I want to set some global variables inside a function, how can I do that? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I am facing an error after performing this syntax: This is the error: Error in file(file, "rt") : cannot ... No such file or directory Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    How can I remove rows which are duplicate in a column. I have read csv file into R data.frame. For ex. ... same data in first column. Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
...