Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在r中,将列表列表合并为一个列表_R_List - Fatal编程技术网

在r中,将列表列表合并为一个列表

在r中,将列表列表合并为一个列表,r,list,R,List,我找不到可以得到所有子列表条目的列表吗?下面是一个简单的示例,列表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]

我找不到可以得到所有子列表条目的列表吗?下面是一个简单的示例,列表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"

listoflists我们可以使用
do.call中的concatenate函数(
c
)来展平嵌套的
列表

res <- do.call(c, listoflists)
all.equal(listofvectors, res, check.attributes = FALSE)
#[1] TRUE

对于没有太多嵌套的特定示例,您也可以使用“purrr”包中的
flant

但是,您没有指定嵌套更深的列表的预期行为。如果您想完全展平嵌套更深的列表,可以通过查看函数

例如:

LL <- list(listoflists, listoflists)
str(flatten(LL))  ## Same as `do.call(c, LL)`
# List of 6
#  $ A:List of 2
#   ..$ : num [1:3] 1 2 3
#   ..$ : num [1:3] 2 34 2
#  $ B:List of 2
#   ..$ : num [1:2] 1 2
#   ..$ : num [1:4] 2 3 2 1
#  $ C:List of 1
#   ..$ : chr [1:3] "sdf" "3" "2"
#  $ A:List of 2
#   ..$ : num [1:3] 1 2 3
#   ..$ : num [1:3] 2 34 2
#  $ B:List of 2
#   ..$ : num [1:2] 1 2
#   ..$ : num [1:4] 2 3 2 1
#  $ C:List of 1
#   ..$ : chr [1:3] "sdf" "3" "2"
str(LinearizeNestedList(LL))
# List of 10
#  $ 1/A/1: num [1:3] 1 2 3
#  $ 1/A/2: num [1:3] 2 34 2
#  $ 1/B/1: num [1:2] 1 2
#  $ 1/B/2: num [1:4] 2 3 2 1
#  $ 1/C/1: chr [1:3] "sdf" "3" "2"
#  $ 2/A/1: num [1:3] 1 2 3
#  $ 2/A/2: num [1:3] 2 34 2
#  $ 2/B/1: num [1:2] 1 2
#  $ 2/B/2: num [1:4] 2 3 2 1
#  $ 2/C/1: chr [1:3] "sdf" "3" "2"
LL或
unlist(listoflists,recursive=FALSE)
purrr::flant(listoflists)
应该这样做。
unlist(listoflists, recursive = FALSE)
library(purrr)
flatten(listoflists)
LL <- list(listoflists, listoflists)
str(flatten(LL))  ## Same as `do.call(c, LL)`
# List of 6
#  $ A:List of 2
#   ..$ : num [1:3] 1 2 3
#   ..$ : num [1:3] 2 34 2
#  $ B:List of 2
#   ..$ : num [1:2] 1 2
#   ..$ : num [1:4] 2 3 2 1
#  $ C:List of 1
#   ..$ : chr [1:3] "sdf" "3" "2"
#  $ A:List of 2
#   ..$ : num [1:3] 1 2 3
#   ..$ : num [1:3] 2 34 2
#  $ B:List of 2
#   ..$ : num [1:2] 1 2
#   ..$ : num [1:4] 2 3 2 1
#  $ C:List of 1
#   ..$ : chr [1:3] "sdf" "3" "2"
str(LinearizeNestedList(LL))
# List of 10
#  $ 1/A/1: num [1:3] 1 2 3
#  $ 1/A/2: num [1:3] 2 34 2
#  $ 1/B/1: num [1:2] 1 2
#  $ 1/B/2: num [1:4] 2 3 2 1
#  $ 1/C/1: chr [1:3] "sdf" "3" "2"
#  $ 2/A/1: num [1:3] 1 2 3
#  $ 2/A/2: num [1:3] 2 34 2
#  $ 2/B/1: num [1:2] 1 2
#  $ 2/B/2: num [1:4] 2 3 2 1
#  $ 2/C/1: chr [1:3] "sdf" "3" "2"