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

Henrik Bengtsson hb at biostat.ucsf.edu
Fri Apr 6 19:44:37 CEST 2012

```str() is your number one friend in R.  Do str(A) and str(A2) after
allocating the matrices and you'll be "surprised".  My \$.02 /Henrik

On Fri, Apr 6, 2012 at 7: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