2-column source->target dataframe to link and node lists?
Is there a native munging function for getting a 2-column (source->target) data.frame into a link list and node list format?
The 2-column dataframe has the following format
| Source | Target |
|---|---|
| A | C |
| A | D |
| B | E |
| B | F |
The link list would be:
| from | to |
|---|---|
| 0 | 2 |
| 0 | 3 |
| 1 | 4 |
| 1 | 5 |
and the node list would be
| id | node |
|---|---|
| 0 | A |
| 1 | B |
| 2 | C |
| 3 | D |
| 4 | E |
| 5 | F |
I can get the data into this format but I want to know if there are standard methods for doing so?
At the moment, no, there is no exported function to do specifically that task. The simpleNetwork function does that internally before passing on to forceNetwork with...
# create nodes data
node_names <- factor(sort(unique(c(as.character(sources), as.character(targets)))))
nodes <- data.frame(name = node_names, group = 1, size = 8)
# create links data
links <- data.frame(source = match(sources, node_names) - 1,
target = match(targets, node_names) - 1,
value = 1)
I’ve been working on a major overhaul of networkD3 that has an exported data conversion function that would handle that situation, among many other data types, automatically, but I haven’t committed any of it yet. It’s likely that igraph and/or tidygraph have something like that too.