[英]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标识符对象
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.