R:根据父子关系创建聚合结构

我想通过使用父子关系(如此数据帧中的关系)在数据库中创建聚合结构:

df <- data.frame(Child=c("C","C11","C111","C112","C12","C121","C122","C123","C13","C131","C132"),
                 Parent=c("","C","C11","C11","C","C12","C12","C12","C","C13","C13"))

从这个父子数据框中,我想得到这样的列表(供在函数中进一步使用):

list.C <- c("C11","C12","C13")
list.C11 <- c("C111","C112")
list.C12 <- c("C121","C122","C123")
list.C13 <- c("C131","C132")

我一直在尝试创建带有循环线和赋值函数的列表,但是没有用……如何创建此列表或列表向量不知道吗?

谢谢!

评论
  • svelit
    svelit 回复

    We can use split in base R

    split(as.character(df$Child), df$Parent)
    

    如果我们想删除“父母”中的空白

    lst1 <- with(subset(df, Parent != ''), split(as.character(Child),
                 droplevels(Parent)))
    
  • _樱花巷
    _樱花巷 回复

    You don't need a for loop. I can propose you a data.table solution.

    library(data.table)
    setDT(df)
    

    如果您希望输出为数据框:

     df[,.(Child = list(Child)), by = Parent]
       Parent          Child
    1:                     C
    2:      C    C11,C12,C13
    3:    C11      C111,C112
    4:    C12 C121,C122,C123
    5:    C13      C131,C132
    

    如果您更喜欢列表:

    list_parents <- split(
      df[,.(list(Child)), by = Parent],
      by = "Parent"
    )
    list_parents
    [[1]]
       Parent Child
    1:            C
    
    $C
       Parent       Child
    1:      C C11,C12,C13
    
    $C11
       Parent     Child
    1:    C11 C111,C112
    
    $C12
       Parent          Child
    1:    C12 C121,C122,C123
    
    $C13
       Parent     Child
    1:    C13 C131,C132