Reputation: 23
I have a data frame with a column which holds parent/child pairs except for the first row. It looks like this:
test<-c("0", "0/00", "00/000", "00/001", "001/001.01", "001.01/001.012", "001/001.0601", "001/001.089", "001/001.09", "001/001.1")
testdf <- data.frame(test)
test
1 0
2 0/00
3 00/000
4 00/001
5 001/001.01
6 001.01/001.012
7 001/001.0601
8 001/001.089
9 001/001.09
10 001/001.1
The first row consists of the root "0". The desired output would look like this:
test
1 0
2 0/00
3 0/00/000
4 0/00/001
5 0/00/001/001.01
6 0/00/001/001.01/001.012
7 0/00/001/001.0601
8 0/00/001/001.089
9 0/00/001/001.09
10 0/00/001/001.1
I'd like to visualize this data as a tree using igraph. Thanks!
Upvotes: 2
Views: 224
Reputation: 37661
Basically, you have an edgelist, so you just need to massage the data into a form that can be used by graph_from_edgelist
. That is just a little string manipulation.
library(igraph)
Pairs = as.character(testdf$test[grep("/", testdf$test)])
EL = matrix(unlist(strsplit(Pairs, "/")), ncol=2, byrow=TRUE)
G = graph_from_edgelist(EL)
LO = layout_as_tree(G, root="0")
plot(G, layout=LO)
To get your texty version of the paths, you can use:
sapply(shortest_paths(G, "0", V(G))$vpath,
function(x) paste(x$name, collapse="/"))
[1] "0" "0/00"
[3] "0/00/000" "0/00/001"
[5] "0/00/001/001.01" "0/00/001/001.01/001.012"
[7] "0/00/001/001.0601" "0/00/001/001.089"
[9] "0/00/001/001.09" "0/00/001/001.1"
Upvotes: 1