本文介绍了具有特定值的行名和列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个矩阵,我想得到一个两列的矩阵,其中一列具有行名,另一列具有值为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更多推荐
具有特定值的行名和列名
发布评论