如何让igraph忽略空白细胞?

时间:2018-04-30 18:32:35

标签: r graph neural-network igraph

所以我有这个数据框,我们称之为test_a2。我想用igraph来创建网络地图。

Col 1      Col 2     Col 3    Col 4

 Table A | Table B | Table C | 

 Table Z | Table A | Table C | Table Y

 Table K | Table L | Table M | Table B

 Table J | Table H | 

我目前正在使用以下代码来映射多列

plot(graph.data.frame(rbindlist(lapply(seq(ncol(test_a2)-1), function(i) test_a2[i:(i+1)]))))

这给我一个包含节点和边的图。但是,如果存在空白空间,则会为其创建节点并创建不必要的连接。无论如何要让它忽略这个?

1 个答案:

答案 0 :(得分:0)

这会有用吗?

library(igraph)
library(data.table)
test_a2 <- data.frame(col1 = c("A","Z","K","J"),
                      col2 = c("B","A","L","H"),
                      col3 = c("C","C","M",""),
                      col3 = c("","Y","B",""), stringsAsFactors=FALSE)
test_a2[test_a2 ==""] <- NA

test_a3 <- na.omit(rbindlist(lapply(seq(ncol(test_a2)-1), function(i) test_a2[i:(i+1)])))
plot(graph.data.frame(test_a3))][1]][1]

Graph

关于这种方法的一个注意事项:图形不包含除了“空”单元格之外没有其他任何东西连接的顶点。如果您需要包含它们,可以在以后添加它们。