rownames and colnames with specific value

前端 未结 3 1146
南方客
南方客 2021-01-22 15:26

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
         


        
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-22 15:38

    Here's one line answer

    x
    ##    X1 X2 X3
    ## X1  1  0  1
    ## X2  0  1  0
    ## X3  0  1  1
    ## X4  1  0  0
    
    
    cbind(rownames(x)[row(x) * x], colnames(x)[col(x) * x])
    ##      [,1] [,2]
    ## [1,] "X1" "X1"
    ## [2,] "X4" "X1"
    ## [3,] "X2" "X2"
    ## [4,] "X3" "X2"
    ## [5,] "X1" "X3"
    ## [6,] "X3" "X3"
    

提交回复
热议问题