我找不到可以获得所有条目列表的列表?这是一个简单的例子,列表o列出:
listoflists <- list("A"=list(c(1,2,3),c(2,34,2)), "B" = list(c(1,2),c(2,3,2,1)), "C" = list(c("sdf",3,2)))
$A
$A[[1]]
[1] 1 2 3
$A[[2]]
[1] 2 34 2
$B
$B[[1]]
[1] 1 2
$B[[2]]
[1] 2 3 2 1
$C
$C[[1]]
[1] "sdf" "3" "2"
Run Code Online (Sandbox Code Playgroud)
我只是通过使用for循环找到了这种悲伤的方式:
listofvectors <- list()
for (i in 1:length(listoflists)) {listofvectors <- c(listofvectors, listoflists[[i]])}
Run Code Online (Sandbox Code Playgroud)
我们可以使用内部的连接函数(c)do.call来展平嵌套list
res <- do.call(c, listoflists)
all.equal(listofvectors, res, check.attributes = FALSE)
#[1] TRUE
Run Code Online (Sandbox Code Playgroud)
或者,正如在评论中提到@Db,unlist用recursive = FALSE也能正常工作
unlist(listoflists, recursive = FALSE)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6191 次 |
| 最近记录: |