0

I have a species by species interaction network, with predator identity in the first row and prey identity in the first column. The matrix is filles with 0 or 1 representing the presence or absence of an interaction. I would like to convert this matrix into a dataframe with two columns, the first column containing the identity of the predator and the second the identity of the prey. Thus each row would be defined by a different predator-prey combinaison, representing a unique interaction. Here is how it should look like:

int.matrix <- matrix(c(1,1,0,1,0,1,0,0,1,0,0,1,0,1,0,0),nrow=4,ncol=4)
rownames(int.matrix)<-c("prey1","prey2","prey3","prey4")
colnames(int.matrix)<-c("pred1","pred2","pred3","pred4")
int.matrix

        pred1 pred2 pred3 pred4
prey1     1     0     1     0
prey2     1     1     0     1
prey3     0     0     0     0
prey4     1     0     1     0

L1 <- c("pred1","pred1","pred1","pred2","pred3","pred3","pred4")
L2 <- c("prey1","prey2","prey4","prey2","prey1","prey4","prey2")
data.frame(cbind(L1,L2))

     L1    L2
1 pred1 prey1
2 pred1 prey2
3 pred1 prey4
4 pred2 prey2
5 pred3 prey1
6 pred3 prey4
7 pred4 prey2

Thank you very much

Valerie

user34771
  • 455
  • 1
  • 4
  • 15

2 Answers2

0

You could use as.table and data.frame, like this:

> x <- data.frame(as.table(int.matrix))
> x[x$Freq == 1, 1:2]
    Var1  Var2
1  prey1 pred1
2  prey2 pred1
4  prey4 pred1
6  prey2 pred2
9  prey1 pred3
12 prey4 pred3
14 prey2 pred4

When you use data.frame on a table, you get a "long" data.frame. Since this data.frame would also include "0" values, you select just the rows where the Freq column is equal to 1.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
0

You may also try:

indx <- !!int.matrix
data.frame(Var1=rownames(indx)[row(indx)][indx], Var2=colnames(indx)[col(indx)][indx])
#   Var1  Var2
#1 prey1 pred1
#2 prey2 pred1
#3 prey4 pred1
#4 prey2 pred2
#5 prey1 pred3
#6 prey4 pred3
#7 prey2 pred4

Or

indx <- (1:4)*int.matrix 
data.frame(Var1=row.names(int.matrix)[indx], Var2=colnames(int.matrix)[indx])

Or

library(reshape2)
res <- melt(!!int.matrix)
res[res$value,-3]
akrun
  • 874,273
  • 37
  • 540
  • 662