I have a logical matrix as follows:
none ants beeswasps grasshoppers flies maggots beetles other
[1,] T F F F F F F F
[2,] T F F F F F F F
[3,] T F F F F F F F
[4,] T F F F F F F F
[5,] T F F F F F F F
[6,] T F F F F F F F
[7,] F F T F F F T T
I want to change the elements with the respective column name when the element is true
, and no text othewise. So, the resultant matrix would be:
none ants beeswasps grasshoppers flies maggots beetles other
[1,] none
[2,] none
[3,] none
[4,] none
[5,] none
[6,] none
[7,] beeswasps beetles other
This is a large matrix with many T/F
values so a robust method is needed, not just creating another matrix to look like the one I want.
Here is a snippet
C = matrix(
c(T,T,T,T,T,T,F,F,F,F,F,F,F,F,F,F,F,F,F,F,T,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,T,F,F,F,F,F,F,T),
nrow=7, ncol=8)
colnames(C) <- c("none", "ants", "beeswasps", "grasshoppers", "flies", "maggots", "beetles", "other")