[英]How to flatten a list of lists?
The tm
package extends c
so that, if given a set of PlainTextDocument
s it automatically creates a Corpus
. tm
包扩展了c
因此,如果给定一组PlainTextDocument
它会自动创建一个Corpus
。 Unfortunately, it appears that each PlainTextDocument
must be specified separately.不幸的是,似乎每个PlainTextDocument
必须单独指定。
eg if I had:例如,如果我有:
foolist <- list(a, b, c); # where a,b,c are PlainTextDocument objects
I'd do this to get a Corpus
:我会这样做以获得Corpus
:
foocorpus <- c(foolist[[1]], foolist[[2]], foolist[[3]]);
I have a list of lists of 'PlainTextDocument
s that looks like this:我有一个'PlainTextDocument
s 列表,如下所示:
> 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
So, to get all my PlainTextDocument
s into a Corpus
, this would work:因此,要将我所有的PlainTextDocument
放入Corpus
,这将起作用:
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)
produces a flat list of PlainTextDocuments, which still leaves me with the problem of feeding a list element by element to c
ETA: foo<-unlist(foolist, recursive=FALSE)
生成一个foo<-unlist(foolist, recursive=FALSE)
的平面列表,这仍然给我一个按元素提供列表元素给c
I expect that unlist(foolist)
will help you.我希望unlist(foolist)
会帮助你。 It has an option recursive
which is TRUE
by default.它有一个选项recursive
,默认情况下为TRUE
。
So unlist(foolist, recursive = FALSE)
will return the list of the documents, and then you can combine them by:所以unlist(foolist, recursive = FALSE)
将返回文档列表,然后您可以通过以下方式组合它们:
do.call(c, unlist(foolist, recursive=FALSE))
do.call
just applies the function c
to the elements of the obtained list do.call
只是将函数c
应用于获得的列表的元素
Here's a more general solution for when lists are nested multiple times and the amount of nesting differs between elements of the lists:当列表多次嵌套并且列表元素之间的嵌套量不同时,这是一个更通用的解决方案:
flattenlist <- function(x){
morelists <- sapply(x, function(xprime) class(xprime)[1]=="list")
out <- c(x[!morelists], unlist(x[morelists], recursive=FALSE))
if(sum(morelists)){
Recall(out)
}else{
return(out)
}
}
Here's another method that worked for my list of lists.这是另一种适用于我的列表列表的方法。
df <- as.data.frame(do.call(rbind, lapply(foolist, as.data.frame)))
Or take a look at new functions in tidyr which work well.或者看看 tidyr 中运行良好的新功能。
rectangle a nested list into a tidy tibble将嵌套列表矩形化为整洁的小标题
lst <- list(
list(
age = 23,
gender = "Male",
city = "Sydney"
),
list(
age = 21,
gender = "Female",
city = "Cairns"
)
)
tib <- tibble(lst) %>%
unnest_wider(lst)
df <- as.data.frame(tib)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.