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

3 messages
Open this post in threaded view
|
Report Content as Inappropriate

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

 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]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Sincere inquiry about “subscript out of bounds” error in R

 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, 卢永芳 <[hidden email]> 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]] > > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.