2

In the example below I am trying to convert the df dataframe into the goal_list list. I am having trouble making it match the exact structure and am still new to dealing with lists.

Example

library(tidyverse)  
library(data.tree)

df <- dplyr::bind_cols(Manager = c('Robert Baratheon','Robert Baratheon','Robert Baratheon','Robert Baratheon','Robert Baratheon','Robert Baratheon','Robert Baratheon','Eddard Stark','Jory Cassel','Barristan Selmy','Barristan Selmy','Barristan Selmy','Barristan Selmy','Barristan Selmy','Barristan Selmy'),  
                       Employee = c('Eddard Stark', 'Pycelle','Petyr Baelish','Renly Baratheon','Stannis Baratheon','Varys','Barristan Selmy','Jory Cassel','Alyn','Jamie Lannister','Meryn Trant','Mandon Moore','Boros Blount','Preston Greenfield','Arys Oakheart'))



Robert <- Node$new("Robert Baratheon")  
Eddard <- Robert$AddChild("Eddard Stark")  
Jory <- Eddard$AddChild("Jory Cassel")  
Alyn <- Jory$AddChild("Alyn")  
Barristan <- Robert$AddChild("Barristan Selmy")  
Jamie <- Barristan$AddChild("Jamie Lannister")  
Meryn <- Barristan$AddChild("Meryn Trant")  
Mandon <- Barristan$AddChild("Mandon Moore")  
Boros <- Barristan$AddChild("Boros Blount")  
Preston <- Barristan$AddChild("Preston Greenfield")  
Arys <- Barristan$AddChild("Arys Oakheart")  
Pycelle <- Robert$AddChild("Pycelle")  
Petyr <- Robert$AddChild("Petyr Baelish")  
Renly <- Robert$AddChild("Renly Baratheon")  
Stannis <- Robert$AddChild("Stannis Baratheon")  
Varys <- Robert$AddChild("Varys")  

goal_list <- ToListSimple(Robert)
Sobo
  • 113
  • 1
  • 8

1 Answers1

0
library(tidyverse)  


df <- dplyr::bind_cols(Manager = c('Robert Baratheon','Robert 

Baratheon','Robert Baratheon','Robert Baratheon','Robert Baratheon','Robert Baratheon','Robert Baratheon','Eddard Stark','Jory Cassel','Barristan Selmy','Barristan Selmy','Barristan Selmy','Barristan Selmy','Barristan Selmy','Barristan Selmy'),  
                       Employee = c('Eddard Stark', 'Pycelle','Petyr Baelish','Renly Baratheon','Stannis Baratheon','Varys','Barristan Selmy','Jory Cassel','Alyn','Jamie Lannister','Meryn Trant','Mandon Moore','Boros Blount','Preston Greenfield','Arys Oakheart'))

This hierarchy_list function will return a nest list of 4 items. Within each list item are two things:

  • The Manager's name
  • A vector with the employees that work for that manager

The output of the function on your data.frame follows:

     hierarchy_list <- function(data.frame){
                           managers <- unique(data.frame$Manager)
                           work_teams <- lapply(managers, function(x) list('Manager' = x,
                                              'Reports' = data.frame[data.frame$Manager == x, 'Employee']))
       return(work_teams)
       }

       x <- hierarchy_list(df)  


> x
[[1]]
[[1]]$Manager
[1] "Robert Baratheon"

[[1]]$Reports
# A tibble: 7 x 1
  Employee         
  <chr>            
1 Eddard Stark     
2 Pycelle          
3 Petyr Baelish    
4 Renly Baratheon  
5 Stannis Baratheon
6 Varys            
7 Barristan Selmy  

[[2]]
[[2]]$Manager
[1] "Eddard Stark"

[[2]]$Reports
# A tibble: 1 x 1
  Employee   
  <chr>      
1 Jory Cassel


[[3]]
[[3]]$Manager
[1] "Jory Cassel"

[[3]]$Reports
# A tibble: 1 x 1
  Employee
  <chr>   
1 Alyn    


[[4]]
[[4]]$Manager
[1] "Barristan Selmy"

[[4]]$Reports
# A tibble: 6 x 1
  Employee          
  <chr>             
1 Jamie Lannister   
2 Meryn Trant       
3 Mandon Moore      
4 Boros Blount      
5 Preston Greenfield
6 Arys Oakheart     
Jorge
  • 392
  • 3
  • 14
  • This still doesn't get me to the desired list structure unfortunately. I am looking for a way to make it *exactly* the same as the goal_list object – Sobo Apr 18 '18 at 05:18