[R] Error: "subscript out of bounds"

Jagat.K.Sheth at wellsfargo.com Jagat.K.Sheth at wellsfargo.com
Mon Dec 1 17:32:34 CET 2008


Note that length(m) = 16, but your m is only 4x4.

Try this m[is.na(m)] <- 0 

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Alex99
Sent: Monday, December 01, 2008 9:06 AM
To: r-help at r-project.org
Subject: [R] Error: "subscript out of bounds"


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-tp20773650p
20773650.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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.



More information about the R-help mailing list