我有以下数据框:
output <- list(structure(list(member = structure(list(name = c("Mick.HMSC-ad",
"John.HMSC-ad", "Paul.HMSC-ad"), band = c("Stones", "Beatles",
"Beatles")), .Names = c("name", "band"), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -3L)), instrument = structure(list(
name = c("John", "Paul", "Keith"), plays = c("guitar.HMSC-ad",
"bass.HMSC-ad", "guitar.HMSC-ad")), .Names = c("name", "plays"
), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-3L))), .Names = c("member", "instrument")), structure(list(member = structure(list(
name = c("Mick.HMSC-bm", "John.HMSC-bm", "Paul.HMSC-bm"),
band = c("Stones", "Beatles", "Beatles")), .Names = c("name",
"band"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-3L)), instrument = structure(list(name = c("John", "Paul", "Keith"
), plays = c("guitar.HMSC-bm", "bass.HMSC-bm", "guitar.HMSC-bm"
)), .Names = c("name", "plays"), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -3L))), .Names = c("member", "instrument"
)))
在这种情况下,我有2个列表,但实际上还有更多。
看起来像这样:
> output
[[1]]
[[1]]$member
# A tibble: 3 x 2
name band
<chr> <chr>
1 Mick.HMSC-ad Stones
2 John.HMSC-ad Beatles
3 Paul.HMSC-ad Beatles
[[1]]$instrument
# A tibble: 3 x 2
name plays
<chr> <chr>
1 John guitar.HMSC-ad
2 Paul bass.HMSC-ad
3 Keith guitar.HMSC-ad
[[2]]
[[2]]$member
# A tibble: 3 x 2
name band
<chr> <chr>
1 Mick.HMSC-bm Stones
2 John.HMSC-bm Beatles
3 Paul.HMSC-bm Beatles
[[2]]$instrument
# A tibble: 3 x 2
name plays
<chr> <chr>
1 John guitar.HMSC-bm
2 Paul bass.HMSC-bm
3 Keith guitar.HMSC-bm
我想要做的是提取$member
元素并将它们绑定到一个数据框中:
name band
<chr> <chr>
1 Mick.HMSC-ad Stones
2 John.HMSC-ad Beatles
3 Paul.HMSC-ad Beatles
4 Mick.HMSC-bm Stones
5 John.HMSC-bm Beatles
6 Paul.HMSC-bm Beatles
我该怎么做?
答案 0 :(得分:2)
您可以使用purrr::map_df
从每个子列表中提取member
元素,将各个元素行绑定到数据框(?map_df
)自动:
purrr::map_df(output, ~ .x$member)
# A tibble: 6 x 2
# name band
# <chr> <chr>
#1 Mick.HMSC-ad Stones
#2 John.HMSC-ad Beatles
#3 Paul.HMSC-ad Beatles
#4 Mick.HMSC-bm Stones
#5 John.HMSC-bm Beatles
#6 Paul.HMSC-bm Beatles
使用dplyr
的{{1}}解决方案:
bind_rows
答案 1 :(得分:1)
包含lapply
,do.call
和rbind
的传统基础R方法。仅提取每个列表的member
部分,并rbind
将它们组合在一起。
do.call("rbind", lapply(output, function(x) x[['member']]))
# name band
#1 Mick.HMSC-ad Stones
#2 John.HMSC-ad Beatles
#3 Paul.HMSC-ad Beatles
#4 Mick.HMSC-bm Stones
#5 John.HMSC-bm Beatles
#6 Paul.HMSC-bm Beatles
答案 2 :(得分:1)
我们可以使用rbindlist
data.table
library(data.table)
rbindlist(lapply(output, function(x) x$member))
# name band
#1: Mick.HMSC-ad Stones
#2: John.HMSC-ad Beatles
#3: Paul.HMSC-ad Beatles
#4: Mick.HMSC-bm Stones
#5: John.HMSC-bm Beatles
#6: Paul.HMSC-bm Beatles