Deleting rows and columns in matrix based on values in diagonal in R -
with r: if have square matrix, easiest(as fast) way delete row , column if value on diagonal less five? example
x1 x2 x3 x4 x1 13 15 12 25 x2 23 4 23 34 x3 21 11 1 11 x4 11 22 23 33
the code delete row 2 , column 2, , row 3 , column 3. tried doing for(i in 1:nrow(mx)), deleting rows changes total number of rows.
the final matrix like
x1 x4 x1 13 25 x4 11 33
use diag
function , logical comparison <
, [
subset operator this...
idx <- ! diag(m) < 5 m <- m[ idx , idx ]
e.g.
m # x1 x2 x3 x4 #x1 13 15 12 25 #x2 23 4 23 34 #x3 21 11 1 11 #x4 11 22 23 33 idx <- ! diag(m) < 5 m <- m[ idx , idx ] m # x1 x4 #x1 13 25 #x4 11 33
Comments
Post a Comment