YJZ
YJZ

Reputation: 4204

R data.tree package: how to specify direction (parent to child, or the other way)

the package doc ?FromDataFrameNetwork says how to specify the direction

This can be either climbing (from parent to children) or descending (from child to parent)

Q1. why direction = "descen" doesn't work:

library(data.tree)
data(acme)

x = ToDataFrameNetwork(acme, direction = "climb")
head(x)
# from                       to
# 1  Acme Inc.               Accounting
# 2  Acme Inc.                 Research
# 3  Acme Inc.                       IT
# 4 Accounting             New Software
# 5 Accounting New Accounting Standards
# 6   Research         New Product Line

x = ToDataFrameNetwork(acme, direction = "descen")
# Error in ToDataFrameNetwork(acme, direction = "descen") : 
#         direction descen unknown. Must be either climb or descen.

#of course i can manually make it from child to parent:
x_the_other_way = x[ , c('to', 'from')]
head(x_the_other_way)
#                       to       from
# 1               Accounting  Acme Inc.
# 2                 Research  Acme Inc.
# 3                       IT  Acme Inc.
# 4             New Software Accounting
# 5 New Accounting Standards Accounting
# 6         New Product Line   Research

Q2. how to specify direction when converting a dataframe network into a tree?

xN <- FromDataFrameNetwork(x, direction = "climb")
# Error in FromDataFrameNetwork(x, direction = "climb") : 
#         unused argument (direction = "climb")

Update about Q2: The algorithm will figure out the direction; the user doesn't need to specify. I guess it may figure it out based on "there's only one root"

xN = FromDataFrameNetwork(x)
xN_the_other_way = FromDataFrameNetwork(x_the_other_way)

xN
# levelName
# 1  Acme Inc.                       
# 2   ¦--Accounting                  
# 3   ¦   ¦--New Software            
# 4   ¦   °--New Accounting Standards
# 5   ¦--Research                    
# 6   ¦   ¦--New Product Line        
# 7   ¦   °--New Labs                
# 8   °--IT                          
# 9       ¦--Outsource               
# 10      ¦--Go agile                
# 11      °--Switch to R  

xN_the_other_way
# levelName
# 1  Acme Inc.                       
# 2   ¦--Accounting                  
# 3   ¦   ¦--New Software            
# 4   ¦   °--New Accounting Standards
# 5   ¦--Research                    
# 6   ¦   ¦--New Product Line        
# 7   ¦   °--New Labs                
# 8   °--IT                          
# 9       ¦--Outsource               
# 10      ¦--Go agile                
# 11      °--Switch to R  

Thank you-

Upvotes: 0

Views: 851

Answers (1)

Christoph Glur
Christoph Glur

Reputation: 1244

The help for ToDataFrameNetwork says:

?ToDataFrameNetwork
direction   when converting to a network, should the edges point from root to children ("climb") or from child to parent ("descend")?

So you must specify either climb or descend.

(There is a typo in the error message, it's already fixed in github, not yet on CRAN).

Regarding your second question: Yes, the network provided in the first two columns must be a tree, i.e. it can contain only one root. Hence, it's simple for the algorithm to find out which direction the network is specified in.

Upvotes: 1

Related Questions