当rownames和列名不匹配时,来自R矩阵的Get.Edgelist

时间:2017-04-06 00:55:47

标签: r matrix graph igraph adjacency-list

我的数据框看起来像这样。

Abdominal Pain | Avandaia
Chest Pain     | Warfrin
Flu-Like       | Warfrin
Liver Damage   | Avandaia
Nausea         | Avanadia
Nausea         | Warfrin
....
....

我希望获得如下所示的边缘列表:

for

我之前使用过R的igraph包来get.edgelist有一个邻接矩阵,但我相信在这种情况下,rownames和列名必须匹配。如何使用不同的行名和列名这样的数据框?

谢谢!

1 个答案:

答案 0 :(得分:0)

一些不使用igraph

的解决方案
df <- read.csv(text="Drug,Abdominal pain,Chest pain,Flu-like,Liver Damage,Nausea,Numbness,Swelling
Avandaia,1,0,0,1,1,1,1
Warfrin,0,1,1,0,1,1,1
Flu-like,0,0,0,0,0,0,0
Liver Damage,0,0,0,0,0,0,0
Nausea,0,0,0,0,0,0,0
Numbness,0,0,0,0,0,0,0
Swelling,0,0,0,0,0,0,0
", check.names=FALSE)


library(tidyr)
gather_(df, "Symptom", "Count", setdiff(names(df),"Drug")) %>% 
    filter(Count > 0)

library(data.table)
setDT(df)
melt.data.table(df, measure.vars=setdiff(names(df),"Drug"))[value>0]