Skip to content

Instantly share code, notes, and snippets.

@DataStrategist
Last active July 17, 2016 15:45
Show Gist options
  • Save DataStrategist/a77102065c75c69c22216f43cc3761be to your computer and use it in GitHub Desktop.
Save DataStrategist/a77102065c75c69c22216f43cc3761be to your computer and use it in GitHub Desktop.
This function takes inputs and converts them from a data.frame like FROM TO VALUE into two dataframes: Nodes and Edges. This is suitable for the packages VisNetwork and network3d.
## This function takes inputs and converts them from a data.frame like FROM TO VALUE into two dataframes:
## Nodes and Edges. This is suitable for the packages VisNetwork and network3d.
## output will be a list containing both dataframes
## The indexing is important. By default, the edges will be 1-indexed (for VisNetwork), but if you're using it with
## network3d, change the indexing to 0
easyMode <- function(df,Index=1){
nodes <- data.frame(name=df[,1:2] %>% unlist %>% as.character() %>% unique())
nodes[,1] <- as.character(nodes[,1])
## and match to IDs to make edges
edges <- data.frame(from= match(df[,1],nodes$name),
to= match(df[,2],nodes$name),
value=df[,3])
## indexing
if (Index==0){
edges$from <- as.integer(edges$from - 1)
edges$to <- as.integer(edges$to - 1)
}
list(nodes=nodes,edges=edges)
}
@DataStrategist
Copy link
Author

resolving minor bug

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