# R Error: Subscript Out of Bounds (Example)

In this R tutorial you’ll learn how to **fix the error message subscript out of bounds**.

The article is structured as follows:

Let’s start right away.

## Creating Example Data

First, we’ll have to create some example data:

my_mat <- matrix(1:20, ncol = 5) # Create example matrix my_mat # Print example matrix # [,1] [,2] [,3] [,4] [,5] # [1,] 1 5 9 13 17 # [2,] 2 6 10 14 18 # [3,] 3 7 11 15 19 # [4,] 4 8 12 16 20 |

my_mat <- matrix(1:20, ncol = 5) # Create example matrix my_mat # Print example matrix # [,1] [,2] [,3] [,4] [,5] # [1,] 1 5 9 13 17 # [2,] 2 6 10 14 18 # [3,] 3 7 11 15 19 # [4,] 4 8 12 16 20

Have a look at the previous output of the RStudio console. It shows that our example data is a numeric matrix consisting of four rows and five columns.

## Example: Reproducing & Solving the Error: Subscript Out of Bounds

This Example shows the reason why the error message “subscript out of bounds” occurs.

Let’s assume that we want to extract certain rows or columns of our matrix. Then we could apply the following R code:

my_mat[3, ] # Printing third row of matrix # [1] 3 7 11 15 19 |

my_mat[3, ] # Printing third row of matrix # [1] 3 7 11 15 19

As you can see in the previous output, we extracted the third row of our data set.

Now, let’s assume that we want to extract a row that doesn’t exist in our data (i.e. the 10th row):

my_mat[10, ] # Trying to print tenth row of matrix # Error in my_mat[10, ] : subscript out of bounds |

my_mat[10, ] # Trying to print tenth row of matrix # Error in my_mat[10, ] : subscript out of bounds

Then the R programming language returns the error message “subscript out of bounds”.

In other words: If you are receiving the error message “subscript out of bounds” you should check whether you are trying to use a data element that does not exist in your data.

## Video, Further Resources & Summary

Do you need more info on the R codes of this tutorial? Then you could have a look at the following video which I have published on my YouTube channel. In the video, I show the R code of this tutorial:

**Please accept YouTube cookies to play this video.** By accepting you will be accessing content from YouTube, a service provided by an external third party.

If you accept this notice, your choice will be saved and the page will refresh.

In addition, you might want to have a look at the other articles on my homepage.

On this page you learned how to **solve the error subscript out of bounds** in R programming. In case you have additional questions, let me know in the comments below. Furthermore, please subscribe to my email newsletter in order to receive updates on the newest articles.

## 15 Comments. Leave new

The dimension of the data is 2592 by 2 and I do like to create area weighted value but it gives me an error subscript out of bounds. Kindly please help me to solve the problem, thank you very much.

dim(temp)

[1] 2592 2

for(j in 3:1647) {for (i in 1:2592) {if(temp[i,j]> -290.0) {areaw[i,j]=veca[i]} }}

Error in temp[i, j] : subscript out of bounds

Hey Goitom,

Could you explain what you want to do with this part of the code: for(j in 3:1647) ?

Regards,

Joachim

Dear Joachim,

Thank you very much for your fast replay, the objective is to make a time series plot for a specific region by sub-setting data from a large domain. The data has missing value with -999.9 , the missing value has to be replaced by zero

#36-by-72 boxes and Jan1880-Jan2016=1633 months + lat and lon

areaw=matrix(0,nrow=2592,ncol = 1647)

dim(areaw)

#[1] 2592 1647

areaw[,1]=temp[,1]

areaw[,2]=temp[,2]

#create an area-weight matrix equal to cosine box with data and zero for missing

dim(temp)

#[1] 2592 2

for(j in 3:1647) {for (i in 1:2592) {if(temp[i,j]> -290.0) {areaw[i,j]=veca[i]} }}

Error in temp[i, j] : subscript out of bounds

#

#—- After the error is fixed the next step will be this below script

#area-weight data matrixs first two columns as lat-lon

tempw=areaw*temp

tempw[,1:2]=temp[,1:2]

#create monthly global average vector for 1645 months

#Jan 1880- Jan 2017

avev=colSums(tempw[,3:1647])/colSums(areaw[,3:1647])

#

#

#area-weight data matrixs first two columns as lat-lon

tempw=areaw*temp

tempw[,1:2]=temp[,1:2]

#create monthly global average vector for 1645 months

#Jan 1880- Jan 2017

avev=colSums(tempw[,3:1647])/colSums(areaw[,3:1647])

#

#

timemo=seq(1880,2017,length=1645)

plot(timemo,avev,type=”l”, cex.lab=1.4,

xlab=”Year”, ylab=”Temperature anomaly [oC]”,

main=”Area-weighted global average of monthly SAT anomalies: Jan 1880-Jan 2017″)

abline(lm(avev ~ timemo),col=”blue”,lwd=2)

text(1930,0.7, “Linear trend: 0.69 [oC] per century”,

cex=1.4, col=”blue”)

#

Again thank you very much for your kindly help

If you want to replace the missing values (i.e. -999.9) by 0, you may simply use the following code:

I do not understand what you are trying to do with this part of the code: for(j in 3:1647) {for (i in 1:2592) {if(temp[i,j]> -290.0) {areaw[i,j]=veca[i]} }}

However, maybe the missing value replacement above already solves your problem?

Regards,

Joachim

Dear Joachim,

Thank you very much for your time, the missing data is replaced to zero based on your code. I will late know the remained part and I will come again with a clear figure and question, dear.

Blessed day

Best Regards

Goitom Kelem

Glad it was helpful! 🙂

Regards,

Joachim

Hello!

I am getting this error message in r: “Error in opts[[x]] : subscript out of bounds” when I run One-Way ANOVAs with different variations of the DV and IVs. I used to run the analysis with no problem a few days ago and now none of the one-way ANOVAs I created is running. Not sure if I am missing a package or if low memory storage in r has to do anything with this. Here is the code I am using to run the analysis:

“`{r}

jmv::ANOVA(data = studentsurvey,

dep = ‘SATISFAC_LIKELAB’, #dependent variable

factors = c(‘Grade’), #iv

effectSize = ‘partEta’,

postHoc = c(‘Grade’),

postHocCorr = ‘tukey’,

postHocES = ‘d’,

emMeans = list(‘Grade’),

emmTables = TRUE,

emmPlotError = ‘ci’)

“`

Hey Amanda,

Could you please illustrate the structure of your data? What is returned when you execute the following code?

Regards,

Joachim

Hello,

I tried to run the summary of my SBM Efficiency results but I kept having this error code

Error in `[<-`(`*tmp*`, urefnames, refnames, value = round(lmbd[urefnames, :

subscript out of bounds

All the results showed up in the console though. Please what could be the problem? Any solutions?

Thanks,

Michael.

Hey Michael,

Could you please share your code?

Regards,

Joachim

>##create a “read_data” object

>SBMAgric=Main_InputOutput_Data data_exampleSBMAgricResultsefficiencies(SBMAgricResults)

> slacks(SBMAgricResults)

>summary(SBMAgricResults)

This reply surpasses the earlier one.

Kindly let me know if this meet your request.

Thank you.

Hello Joachim,

I’ve been able to resolve the problem (I reduced the length of the names of my DMUs to fit the boundary).

Thanks.

Glad you found a solution Michael! 🙂

Dear Joachim

I am trying to install Runway package in R ( Blue sky version)

I get this error message

Can you advice?

Grateful

Thambu

Enter one or more numbers, or an empty line to skip updates:

Error: Failed to install ‘runway’ from GitHub:

subscript out of bounds

In addition: Warning message:

In memory.limit(4095) : cannot decrease memory limit: ignored

Error:Error: Failed to install ‘runway’ from GitHub:

subscript out of bounds

Hi Thambu,

I apologize for the delayed reply. I was on a long vacation, so unfortunately I wasn’t able to get back to you earlier. Do you still need help with your syntax?

Regards,

Joachim