新冠病毒-19:使用R将向量元素追加到列表中的列表中

新冠病毒-19:使用R将向量元素追加到列表中的列表中,r,list,vector,append,R,List,Vector,Append,目标 从个人及其联系人的CSV转到组列表(个人列表) 上下文 groups <- list() id <- "13" groups <- "12:14" # get the index of the group in groups of a given ID (code in appendix) igi = index_group_id(groups, id) contacts = as.list(strsplit(groups, &q

目标

从个人及其联系人的CSV转到组列表(个人列表)

上下文

groups <- list()
id <- "13"
groups <- "12:14"

# get the index of the group in groups of a given ID (code in appendix)
igi = index_group_id(groups, id)
contacts = as.list(strsplit(groups, ":"))
groups[[igi]] = append(groups[[igi]], contacts)
我有一个包含新冠病毒-19社交距离数据的CSV,每个人在一个框架中有一行。每行包含一个ID和一个组

ID只是一个整数。组包含所有与太接近的人的ID

e.g.
| ID | GROUP |
| ---- | ----- |
| 9    | -1    |
| 12   | 13    |
| 13   | 12:14 |
| 14   | 13    |
在这种情况下,所需输出将等于:
list(list(9)、list(12,13,14))

我的尝试

groups <- list()
id <- "13"
groups <- "12:14"

# get the index of the group in groups of a given ID (code in appendix)
igi = index_group_id(groups, id)
contacts = as.list(strsplit(groups, ":"))
groups[[igi]] = append(groups[[igi]], contacts)
问题 它似乎为触点创建了一个向量,并将整个向量添加为一个元素。我试图找到一个解决办法,但还没有成功。 我应该如何将联系人附加到组中的组中,以便ID及其联系人位于同一组中

或者你会推荐一种完全不同的方法吗

附录

index_group_id <- function(groups, id){
  
# return index of group that contains the given ID
  for (i in 1:length(groups)){
    if (id %in% groups[i]){
      return(i)
    }
  }
# if ID not present in any group: make new group with only element ID
  groups <<- append(groups, list(id))
  return(length(groups))
}
index\u group\u id