How to find unique items and save into matrix form

Hi all,
I have a file like this

group1   group2  group3  group4
ax       as       we      aw
as       we       rt      ty
aw       aq       yu       pl
aq       qw       oo       se

I need to count unique (pairwise) one and save as a matrix form like this

        group1      group2       group3    group4
group1   4            2            0          1
group2   2            4            1          0
group3   0            1            4          0
group4   1            0            0          4

I used R intersect function for this but I could not able to save the output as a matrix form.

Not a good solution, but you can get he matrix using outer. Add row and column names separately.

outer(colnames(df), colnames(df), Vectorize(function(p, q) length(intersect(df[[p]], df[[q]]))))

Another possibility that I can think of right now is to find a distance matrix with length(intersect(.)) being the function to ind distances (it's not a metric, but I don't think any implementation checks that). The dist from stats does not allow custom functions, so you have to try other packages for this.

If someone posts a another solution, I'll remove mine. I don't like it myself.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.