[R] Sincere inquiry about “subscript out of bounds” error in R

jim holtman jholtman at gmail.com
Fri Apr 6 18:51:43 CEST 2012


You need to do some basic debugging by putting

options(error=utils::recover)

in your startup of R (or just type it in) so that when the error
occurs you get control at the point of the error and can examine all
the variable.  You have some incorrect data that is causing the
subscript error, so you have to determine why.  You are picking up an
index from "X1[i,2]" or "X[i,3]" that is probably not legal.  Should
be easy to find.

On Fri, Apr 6, 2012 at 10:35 AM, 卢永芳 <sswwssfly at 126.com> wrote:
> Hello,experts
> I am working on a simulation of effect of artificial selection on certain population in Animal Breeding.I am new beginner in coding. I have already build a matrix A(500*500) based on this code
> A<-matrix(,500,500)
> for(i in 1:500){
> for(j in 1:500){
> ifelse(i==j,A[i,j]<-1,A[i,j]<-0)
> }
> }
> and I need to caculate A2
>
> base on A and X1(4500*4500).Here are the codes
> A2<-matrix(4500,500)
> for(i in 1:4500){
> for(j in 1:500){
> A2[i,j]<-(A[X1[i,2],j]+A[X1[i,3],j])/2
> }
> }
> and error happened like this:Error in A2[i, j] <- (A[X1[i, 2], j] + A[X1[i, 3], j])/2 : subscript out of bounds
> I check the criculation number in for loop and it is perfect match with the dimension of matrix A and X1. I do not know how this error happened? And anther inportant question is that how can I build a matrix with very larger dimension which can not  allocate in R.Error in matrix(, 45500, 45500) : cannot allocate vector of length 2070250000
>
>
> I am looking forward to hear from you
> Kindest Regards
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.



More information about the R-help mailing list