Subscript Out of Bounds - General Definition and Solution

Subscript out of bounds - general definition and solution?

This is because you try to access an array out of its boundary.

I will show you how you can debug such errors.

  1. I set options(error=recover)
  2. I run reach_full_in <- reachability(krack_full, 'in')
    I 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")
  3. I enter 1 and I get

     Called from: top level 
  4. I type ls() to see my current variables

      1] "*tmp*"           "alter"           "g"               
    "i" "j" "m"
    "reach_mat" "this_node_reach"

Now, I will see the dimensions of my 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 avoid such error, personally I do this :

  • Try to use applyxx function. They are safer than for
  • I use seq_along and not 1:n (1:0)
  • Try to think in a vectorized solution if you can to avoid mat[i,j] index access.

EDIT vectorize the solution

For example, here I see that you don't use the fact that set.vertex.attribute is vectorized.

You can replace:

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

by this:

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

R/Shiny: Subscript out of bounds error despite working in R

Try

output$FeaturePlot <- renderPlot({
req(input$celltype,input$gene)
FeaturePlot(object = get(input$celltype), reduction = "umap", label = TRUE, min.cutoff = 0, features = get(input$gene))
})

Subscript out of bounds error when creating a matrix

v has length 10, so the match(data$AwayTeam, v) can give an index >5 if the away team is >E, but the matrix A only has 5 columns so attempting to set those values falls outside the size of the array. I'd guess either previous csv's only had teams A..E or ncol=5 has changed from a previous ncol=10.

R Error 'Subscript out of bounds' in if statement - explanation and code fix?

I actually found a solution that works. I skipped the whole part of defining a function and simply used the following code and it worked

Dataset_Events[ Dataset_Events["eventInfo_ea"]=="add to cart", ]["eventInfo_el"] <- NA

Still happy to hear though why the suggestions from all of you didn't seem to modify my dataset at all. Thanks a lot though!!!



Related Topics



Leave a reply



Submit