Count unique (pairwise) one and save as a matrix
2
1
Entering edit mode
5.0 years ago
the_cowa ▴ 40

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 • 923 views
ADD COMMENT
2
Entering edit mode
5.0 years ago
zx8754 12k

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
ADD COMMENT
1
Entering edit mode
5.0 years ago

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
ADD COMMENT

Login before adding your answer.

Traffic: 2796 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6