如何拼合列表列表? [英] How to flatten a list of lists?
问题描述
tm
包扩展了c
,因此,如果给定了一组PlainTextDocument
,它将自动创建一个Corpus
.不幸的是,似乎每个PlainTextDocument
必须分别指定.
The tm
package extends c
so that, if given a set of PlainTextDocument
s it automatically creates a Corpus
. Unfortunately, it appears that each PlainTextDocument
must be specified separately.
例如如果我有:
foolist <- list(a, b, c); # where a,b,c are PlainTextDocument objects
我这样做是为了获得Corpus
:
foocorpus <- c(foolist[[1]], foolist[[2]], foolist[[3]]);
我有一个'PlainTextDocument
列表,看起来像这样:
I have a list of lists of 'PlainTextDocument
s that looks like this:
> str(sectioned)
List of 154
$ :List of 6
..$ :Classes 'PlainTextDocument', 'TextDocument', 'character' atomic [1:1] Developing assessment models Developing models
.. .. ..- attr(*, "Author")= chr "John Smith"
.. .. ..- attr(*, "DateTimeStamp")= POSIXlt[1:1], format: "2013-04-30 12:03:49"
.. .. ..- attr(*, "Description")= chr(0)
.. .. ..- attr(*, "Heading")= chr "Research Focus"
.. .. ..- attr(*, "ID")= chr(0)
.. .. ..- attr(*, "Language")= chr(0)
.. .. ..- attr(*, "LocalMetaData")=List of 4
.. .. .. ..$ foo : chr "bar"
.. .. .. ..$ classification: chr "Technician"
.. .. .. ..$ team : chr ""
.. .. .. ..$ supervisor : chr "Bill Jones"
.. .. ..- attr(*, "Origin")= chr "Smith-John_e.txt"
#etc., all sublists have 6 elements
因此,要将我所有的PlainTextDocument
放入Corpus
中,这将起作用:
So, to get all my PlainTextDocument
s into a Corpus
, this would work:
sectioned.Corpus <- c(sectioned[[1]][[1]], sectioned[[1]][[2]], ..., sectioned[[154]][[6]])
请问有人可以建议一种更简单的方法吗?
Can anyone suggest an easier way, please?
ETA:foo<-unlist(foolist, recursive=FALSE)
生成一个PlainTextDocuments的平面列表,这仍然使我面临着将一个列表元素逐个馈送到c
ETA: foo<-unlist(foolist, recursive=FALSE)
produces a flat list of PlainTextDocuments, which still leaves me with the problem of feeding a list element by element to c
推荐答案
我希望unlist(foolist)
会为您提供帮助.它具有选项recursive
,默认情况下为TRUE
.
I expect that unlist(foolist)
will help you. It has an option recursive
which is TRUE
by default.
因此unlist(foolist, recursive = FALSE)
将返回文档列表,然后可以通过以下方式将它们组合在一起:
So unlist(foolist, recursive = FALSE)
will return the list of the documents, and then you can combine them by:
do.call(c, unlist(foolist, recursive=FALSE))
do.call
只是将函数c
应用于获得的列表的元素
do.call
just applies the function c
to the elements of the obtained list
这篇关于如何拼合列表列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!