[英]Array of values to matrix of pairwise evaluations in R
I am trying to convert an array to a matrix where the matrix cell values evaluate if each pairwise values are the same or different. 我正在尝试将数组转换为矩阵,其中矩阵单元格值评估每个成对的值是相同还是不同。
For example: I am looking to convert an array like this: 例如:我正在寻找转换这样的数组:
df = data.frame(m1=c("Apple", "1", "2"), m2=c("Apple", "3", "4"), m3=c("Plum", "5", "6"))
array = df[1,]
to a matrix like this: 像这样的矩阵:
m1 m2 m3
m1 T
m2 T T
m3 F F T
Where "T" denotes that the two values are equal (ie first entry and the second are both equal to "apple") and "F" identifies that they are different. 其中“ T”表示两个值相等(即第一个条目和第二个都等于“ apple”),而“ F”表示它们是不同的。
Thank you! 谢谢!
You can using apply
here with upper.tri
您可以使用
apply
在这里upper.tri
m=apply(array,2,function(x) x==array)
m[upper.tri(m)] <- ''
m
m1 m2 m3
[1,] "TRUE" "" ""
[2,] "TRUE" "TRUE" ""
[3,] "FALSE" "FALSE" "TRUE"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.