minoo - 1 month ago 4

R Question

I have a few matrices that all of them are the same in number of rows and columns and dimanames of them are exactly the same too. I read them like this for example

`a<-read.csv("a.txt",row.names = 1,header=T,sep="\t")`

b<-read.csv("b.txt",row.names = 1,header=T,sep="\t")

c<-read.csv("c.txt",row.names = 1,header=T,sep="\t")

d<-read.csv("d.txt",row.names = 1,header=T,sep="\t")

e<-read.csv("e.txt",row.names = 1,header=T,sep="\t")

Now I want to get similarity index between a & b, a & c,...,b & c, ..., c & d, d

& e using this code

`library(igraph)`

library(BiRewire)

jaccard.index<-birewire.similarity( a,b)

Then I want to save the result as a data frame like this for example

`mat1 mat2 simil.index`

a b 0.9142

a c 0.8126

a d 0.5066

b e 0.9526

I don't know how can I use these separate matrices in a loop and saving the result like that. Anyone help me on this problem?

Answer

Prepare the function to compute pairwise similarities

```
myfun <- function(x, y) {
birewire.similarity(eval(parse(text = x)), eval(parse(text = y)))
}
```

Build the possible combinations (you said your matrices are named as the first 5 letters of the alphabet, but you can put any names in place of `letters[1:5]`

):

```
myletters <- combn(letters[1:5], 2)
```

Build the dataframe binding by column the combinations and the results of the function that is applied to such combinations:

```
data.frame(t(myletters),
simil.index = mapply(myfun, myletters[1,], myletters[2,]))
```