【发布时间】:2018-07-25 11:14:41
【问题描述】:
假设我有一个名为 list_df_A 的带有 data.frame 的嵌套列表,其结构如下:
$ :'data.frame': 1 obs. of 3 variables:
..$ a :chr a1
..$ b :chr b1
..$ c :chr c1
$ :'data.frame': 3 obs. of 3 variables:
..$ a :chr [1:3] a21 a22 a23
..$ b :chr [1:3] b21 b22 b23
..$ c :chr [1:3] c21 c22 c23
$ :'data.frame': 1 obs. of 3 variables:
..$ a :chr a3
..$ b :chr b3
..$ c :chr c3
所以如果我将它们绑定到 data.table/data.frame 中:
list_df_A <- rbindlist(list_df_A)
list_df_A 将如下所示:
a b c
1: a1 a2 a3
2: a21 b21 c21
3: a22 b22 c22
4: a23 b23 c23
5: a3 b3 c3
现在,我有另一个清单。这个列表实际上是一个 json 文件的根目录。 让我称这个列表为 list_root,它具有以下结构:
chr [1:3] "type1" "type2" "type3"
如果我把它做成 data.table/data.frame:
list_root <- as.data.table(list_root)
我得到了这张桌子
V1
1: type1
2: type2
3: type3
现在问题来了:我知道list_root中的type2在list_df_A中有3条记录。这是因为每个“类型”指的是 list_df_A
中的一个数据帧你怎么告诉 R 当它把两个 data.table 绑定在一起时,它会显示这样的东西?
V1 a b c
1: type1 a1 a2 a3
2: type2 a21 a21 a21
3: type2 b22 b22 b22
4: type2 c23 c23 c23
5: type3 a3 b3 c3
从某种意义上说,第2,3,4行属于type2?
【问题讨论】:
-
没有
rbindlist有一个idcol参数来关闭您传递的列表的名称吗?就为了这个目的? -
...如果您所做的只是行绑定,为什么还要继续引用
cbind? -
当我提到 cbind 我想提到列绑定。像最后一个数据框。您可以看到最后一个数据框中的第一列来自 list_root,倒数第二列是 list_df_A。
-
啊。好吧,那我就把你开始的那个列表命名为列表,然后在
rbindlist中使用idcol,你应该已经设置好了。 -
当你提到使用idcol的rbindlist时,我想到了一个主意。对于 list_root,我使用 rbindlist(list_root, use.names= TRUE, fill=TRUE, idcol=TRUE)。然后对于 list_df_A,我使用 rbindlist(list_df_A, use.names= TRUE, fill=TRUE, idcol=TRUE)。现在两个数据框都有一个 .id 列。然后我可以使用例如 data.frame 通过 .id 将它们合并在一起。像 merge(list_df_A, list_root, on=".id")。