Skip to content
Prev 179706 / 398513 Next

if ((x >.2 || x<(-.2)) && (col(x)!=row(x))) {x=x[, -col(x)]}

You are trying to test the equality of a matrix to a scalar, which  
will produce a logical vector. You are also using && in an apparent  
attempt to conjoin a complex object which will probably not give you  
the results you expect in that context either since it would only  
return a single TRUE or FALSE. Use & in situations where you want  
element-wise comparisons of vectors.

You might start by experimenting on a much smaller object and seeing  
how your efforts at indexing could be improved.

 > X <- matrix(c(runif(9)),nrow=3)
 > X
           [,1]      [,2]      [,3]
[1,] 0.4151688 0.2116687 0.6049845
[2,] 0.7924464 0.6624862 0.8444203
[3,] 0.2634175 0.3357537 0.6923846

 > X>.8
       [,1]  [,2]  [,3]
[1,] FALSE FALSE FALSE
[2,] FALSE FALSE  TRUE
[3,] FALSE FALSE FALSE

#Use apply to create a logical vector that flags the unwanted rows:
 > apply(X,1,function(x) max(x) > 0.8)
[1] FALSE  TRUE FALSE

#Now use that construction on both rows and colums
 > X[-apply(X,1,function(x) max(x) > 0.8), -apply(X,2,function(x)  
max(x) > 0.8)]
           [,1]      [,2]
[1,] 0.6624862 0.8444203
[2,] 0.3357537 0.6923846