我有一个嵌套列表,如下所示。这是一个简化的示例,我的原始数据有数百个项目。
mtcars
mtcars <- mtcars[1:15,]
mtcars <- as.data.table(mtcars)
iris
iris <- iris[1:10,]
iris <- as.data.table(iris)
result <- vector("list", 3)
names <- c("Item 1", "Item 2", "Item 3")
names(result) <- names
testList1 <- list(mtcars, iris)
rm(result)
result <- list(Item1 = list(cars = mtcars,
flowers = iris),
Item2 = list(cars = mtcars,
flowers = iris),
Item3 = list(cars = mtcars,
flowers = iris))
我想总结一下结果,这样我就变成了两个data.tables:一个用于flowers
,另一个用于cars
。
我尝试使用,result <- rbindlist(result, use.names = TRUE, fill = TRUE)
但是会导致错误。
理想的结果是:
testList1 <- list(iris, iris, iris)
flowers <- rbindlist(testList1, use.names = TRUE, fill = TRUE)
flowers
testList1 <- list(mtcars, mtcars, mtcars)
cars <- rbindlist(testList1, use.names = TRUE, fill = TRUE)
cars
考虑到原始列表results
比示例大得多,我如何绑定嵌套列表?
您可以使用purrr
的transpose
,并bind_rows
从dplyr
。
library(purrr)
transpose(result) %>% map(dplyr::bind_rows)
#$cars
# mpg cyl disp hp drat wt qsec vs am gear carb
# 1: 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
# 2: 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
# 3: 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
# 4: 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
# 5: 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
#...
#$flowers
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1: 5.1 3.5 1.4 0.2 setosa
# 2: 4.9 3.0 1.4 0.2 setosa
# 3: 4.7 3.2 1.3 0.2 setosa
# 4: 4.6 3.1 1.5 0.2 setosa
# 5: 5.0 3.6 1.4 0.2 setosa
#...
我们可以添加一'id'
列:
transpose(result) %>% map(dplyr::bind_rows, .id = "id")
#$cars
# mpg cyl disp hp drat wt qsec vs am gear carb id
# 1: 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 Item1
# 2: 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 Item1
# 3: 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 Item1
#...
#...
#43: 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 Item3
#44: 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 Item3
#45: 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 Item3
# mpg cyl disp hp drat wt qsec vs am gear carb id
#$flowers
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species id
# 1: 5.1 3.5 1.4 0.2 setosa Item1
# 2: 4.9 3.0 1.4 0.2 setosa Item1
# 3: 4.7 3.2 1.3 0.2 setosa Item1
#...
#29: 4.4 2.9 1.4 0.2 setosa Item3
#30: 4.9 3.1 1.5 0.1 setosa Item3
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species id
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句