1
votes

So I have this dataframe that we will call test_a2. I want to use igraph to create a network map.

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 | 

I am currently using the following code to map multiple columns

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

This give me a graph with nodes and edges. However, where there is an empty space which it creates a node for and create unnecessary connection. Anyway to have it ignore this?

1

1 Answers

0
votes

Would this work?

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

One note about this approach: the graph will not contain vertices that are not connected with anything else but "empty" cells. If you need to include them you can add them afterwards.