简体   繁体   English

R-igraph:将.csv文件转换为图形文件

[英]R - igraph: convert a .csv file into a graph file

I have a .csv file with this structure: 我有一个具有以下结构的.csv文件:

a,b,c,d

a,c

c,b,a

where each line corresponds to a complete undirected graph. 每行对应一个完整的无向图。

That means: for the line 1, I would need an igraph result like: 这意味着:对于第1行,我需要一个igraph结果,例如:

a-b

a-c

a-d

b-c

b-d

c-d

Would you know an easy way to convert my .csv file to a such graph in R? 您知道一种简单的方法将我的.csv文件转换为R中的图形吗?

Not clear if you want a single graph or a list of graphs (1 for each line). 不清楚是要单个图形还是图形列表(每行1个)。

Assuming the latter: 假设后者:

v <- readLines(textConnection("a,b,c,d
a,c
c,b,a"))

library(igraph)
f <- function(x) graph.data.frame(t(combn(strsplit(x,",")[[1]],2)),directed=FALSE)
g <-lapply(v,f)
par(mfrow=c(1,length(g)))
lapply(g,plot)

Assuming the former (not much point in this). 假设是前者(在此没有多大意义)。

g.full <- do.call(graph.union,g)
par(mfrow=c(1,1))
plot(g.full)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM