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

时间:2020-04-08 19:34:33

标签: r parent-child

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

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")

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

谢谢!

2 个答案:

答案 0 :(得分:2)

您不需要for循环。我可以为您提供data.table解决方案。

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

答案 1 :(得分:1)

我们可以在split中使用base R

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

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

lst1 <- with(subset(df, Parent != ''), split(as.character(Child),
             droplevels(Parent)))