a alonglistof of和alonglistofs of区别

由 匿名 (未验证)
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):问题:
I cannot find out can to get a list of all entry of the sublists? Here's a simple example, a list o lists:
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)))
[1] 2 3 2 1
[1] "sdf" "3"
I only found this sad way by using a for-loop:
listofvectors &- list()
for (i in 1:length(listoflists))
{listofvectors &- c(listofvectors, listoflists[[i]])}
We can use the concatenate function (c) within do.call to flatten the nested list
res &- do.call(c, listoflists)
all.equal(listofvectors, res, check.attributes = FALSE)
Or as @d.b mentioned in the comments, unlist with recursive = FALSE can also work
unlist(listoflists, recursive = FALSE)
For your specific example, which does not have much nesting, you can also just use flatten from the "purrr" package.
library(purrr)
flatten(listoflists)
However, you don't specify the behavior you'd expect for more deeply nested lists. If you want to fully flatten a more deeply nested list, you can look at the
function by .
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"Ultimate list of weird and interesting lists on Wikipedia
Posted by TJ on Tuesday May 6, 2008 @ 04:56 PM
[Tags: , , ]
Below is a list of humorous,interesting, or just plain useless lists found on wikipedia.org...
This article provides a list of unusual deaths

我要回帖

更多关于 alistof 的文章

 

随机推荐