Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(data.tree)
- library(igraph)
- taxonomy <- data.frame(
- children = LETTERS[2:13],
- parents = c("A", "A", "B", "B", "C", "C", "C", "E", "E", "G", "H",
- "H"),
- child.level = c(2, 2, 3, 3, 3, 3, 3,4, 4, 4, 4, 4),
- description = c("w", "i", "z", "v", "j", "u", "q", "g", "b", "c", "t",
- "o"),
- stringsAsFactors = FALSE
- )
- # 1. How to convert
- #
- # Note 1: This works on the current master from github
- # devtools::install_github("gluc/data.tree")
- # This will be released to CRAN some time in August
- #
- # Note 2: I'm currently working on a direct conversion from
- # and to igraph. But that's not available yet.
- root <- unique(taxonomy$parents[!(taxonomy$parents %in% taxonomy$children)])
- g <- igraph::graph.data.frame(taxonomy[,1:2], directed = FALSE)
- GetPath <- function(child) {
- paste(names(all_simple_paths(g, from = root, child)[[1]]), collapse = "/")
- }
- taxonomy$pathString <- sapply(taxonomy$children, GetPath)
- taxtree <- as.Node(taxonomy[, -c(1, 2, 3)]
- print(taxtree, "description", "level")
- #2. Get ancestors
- m <- taxtree$Find("C", "H", "M")
- m$path
- m$parent$Get("name", traversal = "ancestor")
- #3. Get description
- m$Get("description", traversal = "ancestor")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement