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 (i.e. first entry and the second are both equal to "apple") and "F" identifies that they are different.
Thank you!
You can using apply
here with 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"