如何将data.frame导入为igraph对象?

时间:2019-05-13 09:39:10

标签: r igraph

我想使用igraph库。我有一个3列的data.frame,看起来像这样:

> head(data)
     var1    var2  corr
1 OTU0001 OTU0004 0.804
2 OTU0001 OTU0014 0.644
3 OTU0004 OTU0014 0.664
4 OTU0001 OTU0016 0.730
5 OTU0004 OTU0016 0.674
6 OTU0014 OTU0016 0.655

是否可以导入?

1 个答案:

答案 0 :(得分:2)

查看手册:https://igraph.org/r/doc/igraph.pdf

您可以使用类似的内容:

library(igraph)

data <- data.frame(var1=c("OUT0001","OUT0001","OUT0004","OUT0001","OUT0004","OUT0014"),
                   var2=c("OUT0004","OUT0014","OUT0014","OUT0016","OUT0016","OUT0016"),
                   corr=c(0.804,0.644,0.664,0.730,0.674,0.655))

g <- graph_from_data_frame(data, directed=TRUE)
plot(g)