具有特定值的行名和列名

编程入门 行业动态 更新时间:2024-10-28 07:19:33
本文介绍了具有特定值的行名和列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这个矩阵,我想得到一个两列的矩阵,其中一列具有行名,另一列具有值为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

更多推荐

具有特定值的行名和列名

本文发布于:2023-10-26 07:58:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529563.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!