折叠R中的列表列表(版本2)

时间:2015-08-23 18:07:22

标签: r

这与我前几天提出的问题相似,但涉及更多细节。假设我有这样的列表

[[25]][[8]]
[[25]][[8]][[1]]
[1] "0" "1" "0"

[[25]][[8]][[2]]
[1] "1" "0" "0"

[[25]][[8]][[3]]
[1] "0" "0" "0"

[[25]][[8]][[4]]
[1] "0" "0" "0"

[[25]][[8]][[5]]
[1] "0" "0" "0"


[[25]][[9]]
[[25]][[9]][[1]]
[1] "1" "0" "0"

[[25]][[9]][[2]]
[1] "1" "0" "0"

[[25]][[9]][[3]]
[1] "0" "0" "0"

[[25]][[9]][[4]]
[1] "0" "0" "0"

[[25]][[9]][[5]]
[1] "0" "0" "0"

我想把它们折叠成一个看起来像这样的列表

[[25]]
[[25]][[1]]
[1] "0" "1" "0"

[[25]][[2]]
[1] "1" "0" "0"

[[25]][[3]]
[1] "0" "0" "0"

[[25]][[4]]
[1] "0" "0" "0"

[[25]][[5]]
[1] "0" "0" "0"


[[26]]
[[26]][[1]]
[1] "1" "0" "0"

[[26]][[2]]
[1] "1" "0" "0"

[[26]][[3]]
[1] "0" "0" "0"

[[26]][[4]]
[1] "0" "0" "0"

[[26]][[5]]
[1] "0" "0" "0"

我将如何做到这一点?我知道另一个问题我使用了某种形式的lapply和unlist with recursive = FALSE,我假设这就是我必须要做的事情。

1 个答案:

答案 0 :(得分:1)

如果您想在列表中剥离一层嵌套,请使用:

unlist(yourList, recursive = FALSE)

当然,请记住将输出分配给某些东西。

这已在问题中表示为可能的方法。