简体   繁体   English

匹配的rownames等于colnames(对称或非对称矩阵)

[英]Matching rownames that are equal to colnames (of a symmetric or asymmetric matrix)

I'm doing a statistical analyses on distance matrices in R and want to compare distances within individuals and between groups. 我正在对R中的距离矩阵进行统计分析,并希望比较个体之间和群体之间的距离。 I have a matrix where some of the colnames are equal to some of the rownames. 我有一个矩阵,其中一些colnames等于一些rownames。 I want to extract the values where this criteria is met (the problem is getting it to work on a asymmetric matrix). 我想提取满足此条件的值(问题是使其在非对称矩阵上工作)。 If the code could save a matrix with logical values where the criteria is met it would be great) 如果代码可以保存具有符合条件的逻辑值的矩阵,那将是很棒的)

An example of a smaller matrix is shown below: 下面显示了一个较小矩阵的示例:

       1         2         3         4
1 0.4966143 0.8359290 0.7319204 0.7579902
3 0.7002979 0.8621343 0.5152356 0.7875813
4 0.7406555 0.8371479 0.7103873 0.5530200

I want it to end up like this 我希望它最终像这样

       1         2         3         4
1    TRUE      FALSE     FALSE     FALSE
3   FALSE      FALSE     TRUE      FALSE
4   FALSE      FALSE     FALSE      TRUE

Would be happy if I could do it without any loops, just vectorized code 如果我能做到没有任何循环,只需矢量化代码,我会很高兴

We can use outer 我们可以使用outer

out <- outer(row.names(m1), colnames(m1), `==`)
dimnames(out) <- dimnames(m1)
out
#     1     2     3     4
#1  TRUE FALSE FALSE FALSE
#3 FALSE FALSE  TRUE FALSE
#4 FALSE FALSE FALSE  TRUE

Or rep licate the rownames and column names to make the length s equal and then do a == rep licate的rownames和列名,使length享有平等,然后做一个==

`dim<-`(row.names(m1)[row(m1)] == colnames(m1)[col(m1)], dim(m1))

NOTE: as @NelsonGon suggested, when we read data ( read.table/read.csv etc.) as a data.frame , the column names can get appended with prefix X as these are non-canonical names ie starting with number. 注意:正如@NelsonGon建议的那样,当我们将数据( read.table/read.csv等)作为data.frame ,列名称可以附加前缀X因为这些是非规范名称,即以数字开头。 To avoid that either use check.names = FALSE argument in the read.table/read.csv or post process by changing the column names 要避免这种情况,请在read.table/read.csv或post过程中使用check.names = FALSE参数,方法是更改​​列名

outer(row.names(df), sub("^X","",names(df)),"==")

assuming 'df' is the data.frame identifier object 假设'df'是data.frame标识符对象

data 数据

m1 <- structure(list(`1` = c(0.4966143, 0.7002979, 0.7406555), 
                     `2` = c(0.835929,  0.8621343, 0.8371479), 
                     `3` = c(0.7319204, 0.5152356, 0.7103873), 
                     `4` = c(0.7579902, 0.7875813, 0.55302)), 
                class = "data.frame", 
                row.names = c("1", "3", "4"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R在对称矩阵中获得最高的x个单元格及其行名/同名? - R get highest x cells and their rownames/colnames in a symmetric matrix? 根据行名和列名排列矩阵 - Arrange a matrix with regard to the rownames and colnames R矩阵到rownames colnames值 - R matrix to rownames colnames values 通过匹配数据框y中的第1列并插入第3列来重命名矩阵x的行名/名称 - Renaming Rownames/Colnames of matrix `x` by matching column 1 in dataframe `y` and inserting column 3 具有成对距离矩阵输出的列名和行名 - colnames and rownames with pairwise distance matrix outputs R数据帧到矩阵,以便列名和行名来自参数 - R dataframe to matrix such that the colnames and rownames are from parameters 计算R中矩阵中设置的同名和行名的平均值 - Calculate mean of colnames and rownames set in matrix in R 按行名和列名访问矩阵,如果不可用,则返回零 - access matrix by rownames and colnames, and return zero if not available 在 R 中,如何对由两个字符列聚合的 data.table 列中的值求和,其中列名和行名等于字符串 output 的矩阵? - In R how do I sum values in a data.table column aggregated by two character columns, with matrix with colnames and rownames equal to strings output? 使用列名和行名在R中用列值填充矩阵 - Fill matrix with column values in R using colnames and rownames
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM