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.
- I set
options(error=recover)
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")I enter 1 and I get
Called from: top level
I type
ls()
to see my current variables1] "*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 thanfor
- I use
seq_along
and not1: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
Dummy Variables from a String Variable
How to Calculate Cumulative Sum
Fitting Several Regression Models With Dplyr
Remove Na Values from a Vector
Control Ggplot2 Legend Look Without Affecting the Plot
Combining Paste() and Expression() Functions in Plot Labels
Pass a Vector of Variables into Lm() Formula
Creating Arbitrary Panes in Ggplot2
Aggregate Multiple Columns At Once
Dplyr: Inner_Join With a Partial String Match
Coalesce Two String Columns With Alternating Missing Values to One
Create Group Names For Consecutive Values
What Do Hjust and Vjust Do When Making a Plot Using Ggplot
Ignore Outliers in Ggplot2 Boxplot