将分层数据框转换为R中的嵌套列表

时间:2018-04-18 02:20:12

标签: r nested-lists

在下面的示例中,我尝试将 df 数据框转换为 goal_list 列表。我无法使其与确切的结构匹配,并且仍然是处理列表的新手。

实施例

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)

1 个答案:

答案 0 :(得分: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'))

此hierarchy_list函数将返回4个项目的嵌套列表。在每个 列表项是两件事:

  • 经理的姓名
  • 包含为该经理工作的员工的向量

data.frame上函数的输出如下:

     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