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.