Skip to content

Instantly share code, notes, and snippets.

@gluc
Created September 14, 2015 23:08
Show Gist options
  • Save gluc/8a7264b75c505274300e to your computer and use it in GitHub Desktop.
Save gluc/8a7264b75c505274300e to your computer and use it in GitHub Desktop.
csv <- "
parent,child,share,aDirectShare
A,B,50,50
A,C,69,69
A,D,56,56
B,E,80,50
B,F,72,50
C,G,45,45
D,H,67,56
D,I,35,35
D,J,90,56
G,K,55,45"
data <- read.csv(textConnection(csv))
library(data.tree)
tree <- FromDataFrameNetwork(network = data)
tree$Do(function(x) x$share <- x$share / 100)
tree$Do(function(x) x$aDirectShare <- x$aDirectShare / 100)
tree$Do(function(x) x$aIndirectShare <- prod(x$Get("share", traversal = "ancestor", filterFun = isNotRoot)), filterFun = function(x) x$level > 2)
tree$Do(function(x) x$aIndirectShare <- 0, filterFun = function(x) x$level == 2)
print(tree, "share", "aDirectShare", "aIndirectShare")
tree$Do(function(x) x$aTotalShare <- x$aDirectShare + x$aIndirectShare)
SetFormat(tree, name = "share", FormatPercent)
SetFormat(tree, name = "aDirectShare", FormatPercent)
SetFormat(tree, name = "aIndirectShare", FormatPercent)
SetFormat(tree, name = "aTotalShare", FormatPercent)
print(tree, "share", "aDirectShare", "aIndirectShare", "aTotalShare")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment