data.tree: data.tree: Hierarchical Data Structures

data.treeR Documentation

data.tree: Hierarchical Data Structures

Description

data.tree is to hierarchical data what data.frame is to tabular data: An extensible, general purpose structure to store, manipulate, and display hierarchical data.

Introduction

Hierarchical data is ubiquitous in statistics and programming (XML, search trees, family trees, classification, file system, etc.). However, no general-use tree data structure is available in R. Where tabular data has data.frame, hierarchical data is often modeled in lists of lists or similar makeshifts. These structures are often difficult to manage. This is where the data.tree package steps in. It lets you build trees of hierarchical data for various uses: to print, to rapid prototype search algorithms, to test out new classification algorithms, and much more.

Tree Traversal

data.tree allows to Traverse trees in various orders (pre-order, post-order, level, etc.), and it lets you run operations on Nodes via Do. Similarly, you can collect and store data while traversing a tree using the Get and the Set methods.

Methods

The package also contains utility functions to Sort, to Prune, to Aggregate and Cumulate and to print in custom formats.

Construction and Conversion

The package also contains many conversions from and to data.tree structures. Check out the see also section of as.Node.

You can construct a tree from a data.frame using as.Node.data.frame, and convert it back using as.data.frame.Node. Similar options exist for list of lists. For more specialized conversions, see as.dendrogram.Node, as.Node.dendrogram, as.phylo.Node and as.Node.phylo

Finally, easy conversion options from and to list, dataframe, JSON, YAML, igraph, ape, rpart, party and more exist:

  • list: both directions

  • dataframe: both directions

  • JSON, YAML: both directions, via lists

  • igraph: from igraph to data.tree

  • ape: both directions

  • rpart: from rpart to data.tree

  • party: from party to data.tree

Node and Reference Semantics

The entry point to the package is Node. Each tree is composed of a number of Nodes, referencing each other.

One of most important things to note about data.tree is that it exhibits reference semantics. In a nutshell, this means that you can modify your tree along the way, without having to reassign it to a variable after each modification. By and large, this is a rather exceptional behavior in R, where value-semantics is king most of the time.

Applications

data.tree is not optimised for computational speed, but for implementation speed. Namely, its memory footprint is relatively large compared to traditional R data structures. However, it can easily handle trees with several thousand nodes, and once a tree is constructed, operations on it are relatively fast. data.tree is always useful when

  • you want to develop and test a new algorithm

  • you want to import and convert tree structures (it imports and exports to list-of-list, data.frame, yaml, json, igraph, dendrogram, phylo and more)

  • you want to play around with data, display it and get an understanding

  • you want to test another package, to compare it with your own results

  • you need to do homework

For a quick overview of the features, read the data.tree vignette by running vignette("data.tree"). For stylized applications, see vignette("applications", package='data.tree')

Author(s)

Maintainer: Christoph Glur christoph.glur@powerpartners.pro (R interface)

Other contributors:

  • Russ Hyde (improve dependencies) [contributor]

  • Chris Hammill (improve getting) [contributor]

  • Facundo Munoz (improve list conversion) [contributor]

  • Markus Wamser (fixed some typos) [contributor]

  • Pierre Formont (additional features) [contributor]

  • Kent Russel (documentation) [contributor]

  • Noam Ross (fixes) [contributor]

  • Duncan Garmonsway (fixes) [contributor]

See Also

Node

For more details, see the data.tree vignette by running: vignette("data.tree")

Examples

data(acme)
print(acme)
acme$attributesAll
acme$count
acme$totalCount
acme$isRoot
acme$height
print(acme, "p", "cost")

outsource <- acme$IT$Outsource
class(outsource)
print(outsource)
outsource$attributes
outsource$isLeaf
outsource$level
outsource$path
outsource$p
outsource$parent$name
outsource$root$name
outsource$expCost <- outsource$p * outsource$cost
print(acme, "expCost")

acme$Get("p")
acme$Do(function(x) x$expCost <- x$p * x$cost)
acme$Get("expCost", filterFun = isLeaf)

ToDataFrameTable(acme, "name", "p", "cost", "level", "pathString")
ToDataFrameTree(acme, "name", "p", "cost", "level")
ToDataFrameNetwork(acme, "p", "cost")



data.tree documentation built on Nov. 13, 2023, 1:08 a.m.