具有特定值的行名和列名 [英] rownames and colnames with specific value
本文介绍了具有特定值的行名和列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个矩阵,我想得到一个两列的矩阵,其中一列具有行名,另一列具有值为1的单元格的同名
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
我想要一个这样的矩阵
# X1 X1
# X1 X3
# X2 X2
# X3 X2
# X3 X3
# X4 X1
这是值== 1的对
预先感谢, 答:
推荐答案
此处是另一种选择:
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
要获得OP显示,我们要在第一列进行排序:
And to get the OP display , we order by the first column:
mm[order(mm$Var1),]
Var1 Var2
1 X1 X1
9 X1 X3
6 X2 X2
7 X3 X2
11 X3 X3
4 X4 X1
在这里,我要垫住你的职责,让我重现:
Here I mat is your imput , that I reproduce :
mat <- data.frame(X1=c(1,0,0,1),X2=c(0,1,1,0),X3=c(1,0,1,0))
rownames(mat)= paste0('X',1:4)
X1 X2 X3
X1 1 0 1
X2 0 1 0
X3 0 1 1
X4 1 0 0
这篇关于具有特定值的行名和列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文