[R] Error: subscript out of bounds

Marc Schwartz MSchwartz at MedAnalytics.com
Tue Jul 20 20:49:20 CEST 2004


On Tue, 2004-07-20 at 13:12, Marie-Pierre Sylvestre wrote:
> Hi
> 
> I am running a simulation that involves a loop calling three 2
> functions that I have written. Everything works fine when the inside
> of the loop is performed up to 1000 times (for (i in 1:750)). 
> 
> However, I sometimes get : ''Error: subscript out of bounds'' if I try
> to increase the loop 'size' to 1000. I am thinking it has to to with
> memory but I am not sure. I have increased my memory size to 512M but
> it does not solve my problem.
> 
> It would take to much place to copy and paste my code here. It would
> be helpful if you could tell me whether my problem may or may not be
> related to memory size.
> 
> Beside, what's the difference between
> 
>      Error: subscript out of bounds
>      Error: subscript out of range  ?
> 
> 
> Regards
> 
> M-P Sylvestre


If this was a memory error, you would probably get a "cannot allocate
..." type of error message.

More than likely, the object upon which you are using the loop has
dimensions which are smaller than the value(s) that your loops are using
for indexing into the object. The use of either dim(object) or
str(object) will give you more information here. When you increase the
loop size, presumably, you have not increased the size of your
underlying object in kind.

For example, if your object (say a matrix) has dimensions of 500 rows
and 10 columns, your loop is trying to index object[510, 12], which is
'out of bounds' for your object.

A search of the R source code using grep suggests that the 'out of
bounds' message is generally used when trying to index (subset) an
object with a value or values that are not correct as I have above. This
could also be a single dimension vector, BTW. For example, trying to
index object[100] when your vector is only 50 elements in size.

In the case of the 'out of range' message, that appears to be typically
used when an argument to a function or other constrained parameter is
above or below the valid range that the argument or parameter may have. 

A scan of where and how the messages are used indicates some
variability, probably as a result of the multiple authors involved.

HTH,

Marc Schwartz




More information about the R-help mailing list