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.
How about double loop:
  

    # example data
    df1 <- read.table(text = "
    group1   group2  group3  group4
    ax       as       we      aw
    as       we       rt      ty
    aw       aq       yu       pl
    aq       qw       oo       se
    ", header = TRUE, stringsAsFactors = FALSE)
    
    # double loop: get length of intersect
    sapply(df1, function(i) 
      sapply(df1, function(j) length(intersect(i, j))))
    
    #        group1 group2 group3 group4
    # group1      4      2      0      1
    # group2      2      4      1      0
    # group3      0      1      4      0
    # group4      1      0      0      4