Esben Eickhardt Esben Eickhardt - 2 months ago 15
R Question

Converting parent-child relations into a tree with attributes

This is probably basic, but I cannot find out how to do this using data.tree. My data has the following structure:

data <- data.frame(ParentFolderID = c("a", "b", "b", "e"),
FolderID = c("b", "d", "e", "f"),
FolderName = c("N1", "N2", "N3", "N4"),
Values1 = c(1:4),
Values2 = c(2,1,4,2))


I need to create a tree with this data, such that the nodes contain the information: FolderName, Value1 and Value2. Using the function FromDataFrameNetwork from data.tree I can make this happen.

FromDataFrameNetwork(test)
levelName
1 a
2 °--b
3 ¦--d
4 °--e
5 °--f


I can, however, not figure out how to add the other information to the tree. Do you have any suggestions? The documentation does not explain this well unfortunately.

Answer Source

Apparantly you just add a vector with the columns that you want to include. I have still not found out how to handle the strings though.

tree <- FromDataFrameNetwork(data, c("FolderName", "Values1", "Values2"))

> print(tree, "Values2", "Values1")
      levelName Values2 Values1
1 a                  NA      NA
2  °--b               2       1
3      ¦--d           1       2
4      °--e           4       3
5          °--f       2       4

With the character vector this does not work.

print(tree, "FolderName")
      levelName FolderName
1 a                     NA
2  °--b                  1
3      ¦--d              2
4      °--e              3
5          °--f          4