[R] Subscript out of Bounds

Duncan Murdoch murdoch.duncan at gmail.com
Thu Apr 28 22:02:51 CEST 2011


On 28/04/2011 3:49 PM, Dat Mai wrote:
> I currently have this code:
>
> for(j in 2:n){
>    for(i in 1:(j-1)){
>
>      # Make sure the inputs are for the matrix "m"
>      input1=rownames(m)[i]
>      input2=colnames(m)[j]
>
>      q=t[(t$Rec1==input1&  t$Rec2==input2),output]
>
>      if(length(q)==0){
>        q=t[(t$Rec1==input2&  t$Rec2==input1),output]
>      }
>
>      m[i,j]=mean(q)
>      m[j,i]=mean(q)
>      m[j,j]=mean(q)
> }}
>
> I already created a 20x20 matrix "m" and have the rows and columns made up:
>
> m=matrix(data=NA, nrow=rl, ncol=rl, dimnames=list(R1=rec.list, R2=rec.list))
>
> the length of a column in the matrix is 20
> the length of n is 430
>
> When I run this, the error: "Subscript is out of Bounds" appears when it
> reaches the m[i,j].
> Checking again, the issue is primarily with "j"
>
> How would I go about fixing this issue (nevermind the "mean(q)", as I don't
> even know if that works and didn't get the chance to explore it)?

Just before you get to that line, insert the following:

print(dim(m))
print(i)
print(j)

Take a look at the results, and see if i is really in the range from 1 
to dim(m)[1], and j in the range 1 to dim(m)[2].  From what you're 
saying, it sounds as though j can go to 430, which is too big, if m is 
20 by 20.

Duncan Murdoch



More information about the R-help mailing list