将列表的索引添加到bind_rows吗? [英] Add the index of list to bind_rows?
本文介绍了将列表的索引添加到bind_rows吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据:
dat=list(structure(list(Group.1 = structure(3:4, .Label = c("A","B", "C", "D", "E", "F"), class = "factor"), Pr1 = c(65, 75)), row.names = c(NA, -2L), class = "data.frame"),NULL, structure(list( Group.1 = structure(3:4, .Label = c("A","B", "C", "D", "E", "F"), class = "factor"), Pr1 = c(81,4)), row.names = c(NA,-2L), class = "data.frame"))
我想通过 bind_rows(dat)
使用合并,但将索引号保持为可变输出包括 Type([[1]]和[[3]])
I want to use combine using bind_rows(dat)
but keeping the index number as a varaible
Output Include Type([[1]] and [[3]])
type Group.1 Pr1
1 1 C 65
2 1 D 75
3 3 C 81
4 3 D 4
推荐答案
data.table解决方案
使用 data.table
-package中的 rbindlist()
,该软件包具有内置的ID支持,尊重NULL df.
use rbindlist()
from the data.table
-package, which had built-in id-support that respects NULL df's.
library(data.table)
rbindlist( dat, idcol = TRUE )
.id Group.1 Pr1
1: 1 C 65
2: 1 D 75
3: 3 C 81
4: 3 D 4
dplyr-部分解决方案
bind_rows也具有ID支持,但是它会跳过"空元素...
bind_rows also has ID-support, but it 'skips' empty elements...
bind_rows( dat, .id = "id" )
id Group.1 Pr1
1 1 C 65
2 1 D 75
3 2 C 81
4 2 D 4
请注意,来自dat的第三个元素的ID变为2,而不是3.
Note that the ID of the third element from dat becomes 2, and not 3.
这篇关于将列表的索引添加到bind_rows吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文