I've found myself in similar situations numerous times, and I've usually just rolled my own solution. I agree that the XML packages are overkill for focused problems such as this. The iGraph package is nice, but beware. It is an interface to a C library and is zero-indexed (unlike R which is 1-indexed). I've used it successfully, but there always seems to be a painful debugging process in which I keep finding "unshifted" indexes. (e.g. foo[index]
has to change to foo[index-1]
.)
Again, I'd probable just roll my own. Something like:
foo <- structure(list(child = 1:20, parent = c(NA, 1L, 2L, 3L, 1L, 5L, 6L, 5L, 8L, 1L, 1L, 11L, 12L, 11L, 14L, 14L, 1L, 17L, 18L, 19L)), .Names = c("child", "parent"), class = "data.frame", row.names = c(NA, -20L))
get_ancestry <- function(node, data=foo, ancestry=c()) {
parent <- data$parent[data$child %in% node]
if (is.na(parent)) {
return(ancestry)
} else {
get_ancestry(parent, data, c(ancestry, parent))
}
}
get_children <- function(nodes, data=foo) {
unlist(lapply(nodes, function(x) {data$child[data$parent %in% x]}))
}
orange <- get_ancestry(13)
yellow <- get_children(orange)