data.tree: Hierarchical Data Structures
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.
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.
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 JSON, YAML, igraph, and more exist.
The entry point to the package is Node
. Each tree is composed of a number of Node
s, 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.
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')
For more details, see the data.tree
vignette by running: vignette("data.tree")
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")
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.