The matrix is specified mas follows (string permutations 1-5):
# [,1] [,2] [,3] [,4] [,5]
# [1,] 1 5 2 4 3
# [2,] 2 1 4 3 5
# [3,] 3 4 1 2 5
# [4,] 4 1 3 2 5
# [5,] 4 3 1 2 5
# [6,] 1 4 2 3 5
# [7,] 4 3 2 5 1
# [8,] 4 1 3 5 2
# [9,] 1 2 3 4 5
# [10,] 4 3 2 1 5
I would like to know how many times each element 1-5 is preceded by another element in the line (i.e. considering all possible pairs)
For example, for a pair (1, 5) it is 1preceded 5, 9 times among all lines. Another example, for a pair (3, 1), is 3preceded 1by 4 times among all rows. I would like to have the same results for all possible pairs among all rows. I.e
m <- structure(c(1L, 2L, 3L, 4L, 4L, 1L, 4L, 4L, 1L, 4L, 5L, 1L, 4L,
1L, 3L, 4L, 3L, 1L, 2L, 3L, 2L, 4L, 1L, 3L, 1L, 2L, 2L, 3L, 3L,
2L, 4L, 3L, 2L, 2L, 2L, 3L, 5L, 5L, 4L, 1L, 3L, 5L, 5L, 5L, 5L,
5L, 1L, 2L, 5L, 5L), .Dim = c(10L, 5L))
How to do this efficiently in R?
EDIT
How would you do the same for this matrix?
m <- structure(c(3, 1, 3, 4, 3, 3, 4, 4, 5, 5, 4, 2, 5, 5, 4, 1, 1,
3, 2, 4, 1, 5, 0, 0, 1, 2, 5, 5, 0, 2, 5, 3, 0, 0, 5, 0, 2, 2,
0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0), .Dim = c(10L, 5L))