Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

r - hierarchical data frame from child/parent relations

I have a child - parent data.frame that I want to transform to a complete hierarchical list with all levels and a level number. The example data below goes to three levels, but it could be more. What function can I use to transform the data?

Source:

data.frame(name = c("land", "water", "air", "car", "bicycle", "boat", "balloon",
  "airplane", "helicopter", "Ford", "BMW", "Airbus"), parent = c(NA, NA, NA, 
  "land", "land", "water", "air", "air", "air", "car", "car", "airplane"))

         name   parent
1        land     <NA>
2       water     <NA>
3         air     <NA>
4         car     land
5     bicycle     land
6        boat    water
7     balloon      air
8    airplane      air
9  helicopter      air
10       Ford      car
11        BMW      car
12     Airbus airplane

Destination:

data.frame(level1 = c("land", "water", "air", "land", "land", "water", "air", 
  "air", "air", "land", "land", "air"), level2 = c(NA, NA, NA, "car", "bicylcle", 
  "boat", "balloon", "airplane", "helicopter", "car", "car", "airplane"),
  level3 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, "Ford", "BMW", "Airbus"), 
  level_number = c(1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3))

   level1     level2 level3 level_number
1    land       <NA>   <NA>            1
2   water       <NA>   <NA>            1
3     air       <NA>   <NA>            1
4    land        car   <NA>            2
5    land   bicylcle   <NA>            2
6   water       boat   <NA>            2
7     air    balloon   <NA>            2
8     air   airplane   <NA>            2
9     air helicopter   <NA>            2
10   land        car   Ford            3
11   land        car    BMW            3
12    air   airplane Airbus            3
like image 698
Timm S. Avatar asked Oct 11 '15 19:10

Timm S.


1 Answers

Using the data.tree package, you could do the following:

library(data.tree)
df <- data.frame(name = c("land", "water", "air", "car", "bicycle", "boat", "balloon", "airplane", "helicopter", "Ford", "BMW", "Airbus"), 
                 parent = c("root", "root", "root", "land", "land", "water", "air", "air", "air", "car", "car", "airplane"))

Note that I replaced the NAs with "root", which makes the conversion to a data.tree much easier. Namely:

tree <- FromDataFrameNetwork(df)

Getting the required format then becomes trivial as we can use the hierarchy infrastructure from data.tree:

ToDataFrameTree(tree, 
                level1 = function(x) x$path[2],
                level2 = function(x) x$path[3],
                level3 = function(x) x$path[4],
                level_number = function(x) x$level - 1)[-1,-1]
like image 63
Christoph Glur Avatar answered Nov 01 '22 22:11

Christoph Glur