You can look at the crossprod
(or tcrossprod
) function along with table
:
out <- tcrossprod(table(df))
out
# Gene
# Gene gene1 gene2 gene3
# gene1 3 2 1
# gene2 2 3 0
# gene3 1 0 2
Drop the diagonal and the lower-triangle to get the exact output you show.
diag(out) <- NA
out[lower.tri(out)] <- NA
print.table(out) ## print.table deals with NAs differently
# Gene
# Gene gene1 gene2 gene3
# gene1 2 1
# gene2 0
# gene3