This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Count unique (pairwise) one and save as a matrix

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.

r

2 answers

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

If an answer was helpful, you should upvote it; if the answer resolved your question, you should mark it as accepted. You can accept more than one if they work.
Upvote|Bookmark|Accept

Your dataframe:

########## 
> head(m)
  group1 group2 group3 group4
1     ax     as     we     aw
2     as     we     rt     ty
3     aw     aq     yu     pl
4     aq     qw     oo     se
##########
> class(m)
"data.frame"

Code to implement:

## all pairwise combos for the number of columns
combos <- combn(seq(ncol(m)),2)

## find all the triangle values
filler <- apply(combos,2,function(x){
  length(intersect(m[,x[[1]]],m[,x[[2]]]))
})

## find the diagonal values
diag <- sapply(lapply(m,unique),length)
## build empty matrix
m[] <- NA
## fill it up
m[upper.tri(m)] <- filler
diag(m) <- diag
m[lower.tri(m)] <- filler
##########

Final output:

##########
> head(m)
  group1 group2 group3 group4
1      4      2      0      1
2      2      4      1      0
3      0      1      4      0
4      1      0      0      4

Log in to answer this question.