Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2-column source->target dataframe to link and node lists? #246

Open
areeves87 opened this issue Jul 30, 2018 · 1 comment
Open

2-column source->target dataframe to link and node lists? #246

areeves87 opened this issue Jul 30, 2018 · 1 comment

Comments

@areeves87
Copy link

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?

@cjyetman
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants