How to prune duplicate nodes to nearest common ancestor? data.tree R

Viewed 69

I have an organisational structure expressed as a tree.

library(data.tree)
library(dplyr)



    df <-data.frame(from = c('BIG CORP Inc.','BIG CORP Inc.', 'BIG CORP Inc.', 'ABC Inc.', 'ABC Inc.', 
                              'Subsidiary 1', 'Subsidiary 1', 'Subsidiary 2', 'Subsidiary 2', 'BCD Inc.', 'CDE Inc.'),
                     to   = c('ABC Inc.',     'BCD Inc.',      'CDE Inc.',  'Subsidiary 1', 'Subsidiary 2',
                              'Subsidiary 3', 'Subsidiary 4', 'Subsidiary 4', 'Subsidiary 3',  'Subsidiary 4', 'Subsidiary 4'),
                     ownership = c(1,1,1, 1, 0.5, 0.5, 0.25, 0.25, 0.5, 0.25, 0.25),
                     cost = c(0, 100,100,100,100,100,100,100,100 ,500, 500))
    
    
    org_str1 <- FromDataFrameNetwork(df)
    
    print(org_str1, "ownership", "cost", "exposure" ,"level")

If you note, the relationship in the tree is that one child can have many parents. As you can see in the image below, Subsidiary 4 sits under 4 unique underlying branches. Similarly Subsidiary 3 sits on 2 unique branches.

Tree Structure

How can I summarize my tree so that wherever a child sits on multiple branches I can view that child under the earliest common ancestor.

So for example Subsidiary 4 would need to sit under BIG CORP as that is the first common ancestor of all branches with Subsidiary 4 in it. Similarly Subsidiary 3 should report directly under ABC Inc.

0 Answers
Related