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

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

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

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.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


6 Comments. Leave new

  • Goitom Kelem
    March 15, 2022 4:30 pm

    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

    Reply
  • Goitom Kelem
    March 16, 2022 7:47 am

    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

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

      temp[temp == -999.9] <- 0

      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

      Reply
      • Goitom Kelem
        March 16, 2022 1:32 pm

        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

        Reply

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top