rownames and colnames with specific value
I have this matrix and i want to get a 2 column matrix, where one column has the rowname and the other column, the colname of cells with value 1 x X1 X2 X3 X1 1 0 1 X2 0 1 0 X3 0 1 1 X4 1 0 0 str(x) num [1:886, 1:886] 1 0 1 1 1 0 1 1 1 1 I want a matrix like this # X1 X1 # X1 X3 # X2 X2 # X3 X2 # X3 X3 # X4 X1 which are the pairs that have value=1 Thanks in advance, A. here another option : mm <- expand.grid(rownames(mat),colnames(mat))[as.vector(mat==1),] Var1 Var2 1 X1 X1 4 X4 X1 6 X2 X2 7 X3 X2 9 X1 X3 11 X3 X3 And to get the OP display , we order by the first column: mm[order(mm$Var1),]