R 未在do.call中将列表名作为行名获取
我有一个数据帧列表,下面是一个示例R 未在do.call中将列表名作为行名获取,r,list,dataframe,do.call,R,List,Dataframe,Do.call,我有一个数据帧列表,下面是一个示例 list(First = structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4), Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9), Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7), Petal.Width = c(0.2, 0.2, 0.2, 0.2, 0.2, 0.4), Species = struc
list(First = structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6,
5, 5.4), Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9), Petal.Length = c(1.4,
1.4, 1.3, 1.5, 1.4, 1.7), Petal.Width = c(0.2, 0.2, 0.2, 0.2,
0.2, 0.4), Species = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("setosa",
"versicolor", "virginica"), class = "factor")), row.names = c("A",
"B", "C", "D", "E", "F"), class = "data.frame"), Second = structure(list(
Sepal.Length = c(6.7, 6.7, 6.3, 6.5, 6.2, 5.9), Sepal.Width = c(3.3,
3, 2.5, 3, 3.4, 3), Petal.Length = c(5.7, 5.2, 5, 5.2, 5.4,
5.1), Petal.Width = c(2.5, 2.3, 1.9, 2, 2.3, 1.8), Species = structure(c(3L,
3L, 3L, 3L, 3L, 3L), .Label = c("setosa", "versicolor", "virginica"
), class = "factor")), row.names = c("A", "B", "C", "D",
"E", "F"), class = "data.frame"))
我希望在do.call()
上获得列表名作为行名,但没有得到预期的结果。所以我希望在合并后将列表名作为行名
do.call(rbind,df)
由于您的数据帧已经有行名,因此您无法将列表名作为行名获取。因此,您需要首先删除行名称,然后合并列表
library(tibble)
df <- lapply(df, function(x){
x <- rownames_to_column(x, var = "col")
})
do.call(rbind, df)
我们可以使用
bind\u行
library(dplyr)
bind_rows(lst1, .id = 'dfID')
也许可以试试:
data.table::rbindlist(df,idcol=“dfID”)
请提供预期的输出,这在您的帖子中不是很清楚。如果我理解正确,您希望列表名称作为行名称吗?@zx8754这很有效。感谢you@AgazWani是的,没错
library(dplyr)
bind_rows(lst1, .id = 'dfID')