# Re: [R] Subscript out of Bounds

From: Duncan Murdoch <murdoch.duncan_at_gmail.com>
Date: Thu, 28 Apr 2011 16:02:51 -0400

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), and j in the range 1 to dim(m). 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

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. Received on Thu 28 Apr 2011 - 20:05:37 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 28 Apr 2011 - 20:50:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.