Reputation: 2570
Imagine you have the matrix mat
:
mat <- matrix(c(0,2,3,3,0,1,1,1,0), nrow = 3,byrow=TRUE)
rownames(mat) <- list("a","b","c")
colnames(mat) <- list("a","b","c")
mat
# a b c
#a 0 2 3
#b 3 0 1
#c 1 1 0
I want to find the which of the upper and lower triangle pairs are the biggest. e.g. b:a
is >
than a:b
and a:c
is >
than c:a
.
I then want to return a mtrix where the cell with the biggest value in the pair gets a 1 and evrything else, smaller values and ties get a zero. The result for mat
would be:
result <- matrix(c(0,0,1,1,0,0,0,0,0), nrow = 3,byrow=TRUE)
rownames(result ) <- list("a","b","c")
colnames(result ) <- list("a","b","c")
result
# a b c
#a 0 0 1
#b 1 0 0
#c 0 0 0
Upvotes: 0
Views: 27
Reputation: 269852
Try this:
> (mat > t(mat)) + 0
a b c
a 0 0 1
b 1 0 0
c 0 0 0
To return 0.5 for ties:
> (mat > t(mat)) + .5 * (mat == t(mat))
a b c
a 0.5 0.0 1.0
b 1.0 0.5 0.5
c 0.0 0.5 0.5
Upvotes: 1