[R] Error: "subscript out of bounds"

Alex99 loyola9988 at yahoo.com
Mon Dec 1 16:05:50 CET 2008


Hi All,

I am trying to replace the "NA" values in a matrix by using the following
function:
it gets a "name" of the matrix or list or vector and turns it to a matrix
called "m". 
then checks the elements of the matrix and if any of them is "NA" replace
them with "0".

rep=function(name){
m=as.matrix(name)
for(i in 1:length(m)){
 for(j in 1:length(m)){
  if(is.na(m[i,j])){
    m[i,j]=0 
}}}}

when I use the function with the following matrix:

      X1  X2  X3    X5
X1  1.00 NA NA -0.25
X2    NA  1 NA    NA
X3    NA NA  1    NA
X5 -0.25 NA NA  1.00

I get this error: "subscript out of bounds".
I used debug on "rep" function and it seems it works for i=1 and j=1&2&3&4&5
after that, when it's suppose to go to i=2 I get the error.
any idea why and how to fix it?
Thanks
-- 
View this message in context: http://www.nabble.com/Error%3A-%22subscript-out-of-bounds%22-tp20773650p20773650.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list