Data
Consider the following matrix.
set.seed(1)
base <- letters[1:4]
mmm <- matrix(sample(base, size=20, replace=TRUE), nrow=4)
print.table(cbind(base, "|", data=mmm))
# base
# [1,] a | a b b a b
# [2,] b | d a b a b
# [3,] c | c c c a b
# [4,] d | a c c b c
Expected output
Matrix indexed by base x base.
| a b c d
a| 2 3 0 0
b| 2 2 0 1
c| 1 1 3 0
d| 1 1 3 0
Element (x,y) contains n if y occurs n times in row x.
Or a list of triplets
a,a,2
a,b,3
b,a,2
...
Update
If NA's are involved, for example mmm[2,1] <- NA, then
table(base[row(mmm)], mmm, useNA="ifany")
# mmm
# a b c <NA>
# a 2 3 0 0
# b 2 2 0 1
# c 1 1 3 0
# d 1 1 3 0
With table
:
table(rep(base, ncol(mmm)), mmm)
#> mmm
#> a b c d
#> a 2 3 0 0
#> b 2 2 0 1
#> c 1 1 3 0
#> d 1 1 3 0