0
votes

I am trying to retain colnames and rownames of matrix after filtering. However I lose both the matrix and the relevant names regardless of using drop=FALSE. Example given below.

mat <- matrix(runif(100),ncol=5)
colnames(mat) = letters[1:5];
rownames(mat)=paste("test",letters[1:20],sep="")
m2 <- mat[(mat >= 0.55), drop=FALSE]

m2
     [1] 0.5836022 0.9931983 0.9923270 0.8296150 0.5848827 0.5587868 0.7914669 0.6888435 0.7184756 0.8201574 0.6157212 0.5979301 0.6789259 0.8648659 0.5700341 0.6313953 0.6824154
    [18] 0.6978662 0.7297806 0.9376626 0.6095106 0.6128999 0.8729137 0.6194508 0.8805783 0.8857406 0.5706092 0.8307489 0.9863317 0.6729674 0.9071986 0.7061558 0.8433151 0.7381484
    [35] 0.8969046 0.7389939 0.9188947 0.6714318 0.7584971 0.7197169 0.9407508 0.7128124 0.6844223 0.6561814 0.9549364 0.9427498 0.9638931 0.6087301

How do I recover my matrix after filtering and also keeping the relevant colnames and rownames. Any suggestion will be very helpful.

1
is.na(mat) <- mat >= 0.55Axeman

1 Answers

0
votes

Found the solution.

mat2 <- ifelse(mat<0.55,NA,mat)