我有一个包含相同元素的列表列表。我希望它们位于包含列表中类似元素的数据框中。
list <- list(school1 = list(grade1 = c(89, 87, 90), grade2 = c(88, 98, 65), grade3 = c(90, 89, 78)), school2 = list(grade1 = c(85, 88, 93), grade2 = c(78, 68, 95), grade3 = c(92, 83, 75)))
所需的输出将是:
grade1 grade2 grade3
89 88 90
87 98 89
90 65 78
85 78 92
88 68 83
93 95 75
答案 0 :(得分:3)
我们可以使用as_tibble
将list
内部的list
转换为tibble
,然后绑定行(map_df
)
library(tidyverse)
map_df(list, as_tibble)
# A tibble: 6 x 3
# grade1 grade2 grade3
# <dbl> <dbl> <dbl>
#1 89 88 90
#2 87 98 89
#3 90 65 78
#4 85 78 92
#5 88 68 83
#6 93 95 75
或与base R
do.call(rbind, lapply(list, as.data.frame))
答案 1 :(得分:0)
另一种尝试是:
do.call( rbind.data.frame, list )
# grade1 grade2 grade3
# school1.1 89 88 90
# school1.2 87 98 89
# school1.3 90 65 78
# school2.4 85 78 92
# school2.5 88 68 83
# school2.6 93 95 75