r - recursively assign unique names to nodes in a data.tree object -
i working list of lists, collected xml chunk, represent object defined data.tree package r. example below seems work, , can extract elements data.tree representation of list of lists. however, cannot figure out how use of text-formatting or visualization options (e.g. igraph) due fact "child" elements of each list not uniquely labeled.
ideally, recursively re-name "children" serial number. example, convert this:
children |-- rulerule |-- rulerule |-- rulerule to this:
children |-- rulerule_01 |-- rulerule_02 |-- rulerule_03 or better, re-name "children" according attribute such
children
|-- rulerule_15976 |-- rulerule_49444 |-- rulerule_15748 here a similar question almost looking for. not sure if use of data.tree functionality simplify re-naming of children elements, or if should done before initializing data.tree object. tree-traversal capabilities of data.tree seem right route, since types of data using can have multiple set of children, @ level.
a self-contained example:
library(data.tree) # typical list l <- structure(list(rulestart = structure(list(children = structure(list( ruleoperator = structure(list(children = structure(list(rulerule = structure(list( children = null, refid = "49446"), .names = c("children", "refid")), rulerule = structure(list(children = null, refid = "15976"), .names = c("children", "refid")), rulerule = structure(list(children = null, refid = "49444"), .names = c("children", "refid")), rulerule = structure(list(children = null, refid = "15748"), .names = c("children", "refid")), rulerule = structure(list(children = null, refid = "49440"), .names = c("children", "refid")), rulerule = structure(list(children = null, refid = "15746"), .names = c("children", "refid")), rulerule = structure(list(children = null, refid = "49449"), .names = c("children", "refid"))), .names = c("rulerule", "rulerule", "rulerule", "rulerule", "rulerule", "rulerule", "rulerule")), type = "product"), .names = c("children", "type"))), .names = "ruleoperator")), .names = "children")), .names = "rulestart") # convert xml list data.tree object n <- fromlistexplicit(l$rulestart, namename=null, childrenname='children') # check print(n, 'refid')
thanks author of data.tree suggestion. following function recursively re-name elements of list. seems work, comments or better solution welcome.
makenamesunique <- function(l) { l.names <- names(l$children) # multiple children types tab <- table(l.names) t.names <- names(tab) # iterate on types for(this.type in seq_along(t.names)) { # iterate on duplicate names # index type idx <- which(l.names == t.names[this.type]) for(this.element in seq_along(idx)) { # make copy of chunk of tree l.sub <- l$children[[idx[this.element]]] # if terminal leaf re-name , continue if(is.null(l.sub$children)) { # print('leaf') names(l$children)[idx[this.element]] <- paste0(t.names[this.type], '_', this.element) } # otherwise re-name , step element , apply function recursively else { # print('branch') names(l$children)[idx[this.element]] <- paste0(t.names[this.type], '_', this.element) # fix branch , splice tree l$children[[idx[this.element]]] <- makenamesunique(l.sub) } } } return(l) }
Comments
Post a Comment