在R中预分配列表 [英] preallocate list in R
本文介绍了在R中预分配列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
R在循环中扩展数据结构的效率很低.如何预分配一定大小的list
? matrix
通过ncol
和nrow
参数使此操作变得容易.如何在清单中做到这一点?例如:
It is inefficient in R to expand a data structure in a loop. How do I preallocate a list
of a certain size? matrix
makes this easy via the ncol
and nrow
arguments. How does one do this in lists? For example:
x <- list()
for (i in 1:10) {
x[[i]] <- i
}
我认为这是无效的.有什么更好的方法?
I presume this is inefficient. What is a better way to do this?
推荐答案
vector
可以创建具有所需模式和长度的空向量.
vector
can create empty vector of the desired mode and length.
x <- vector(mode = "list", length = 10)
这篇关于在R中预分配列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文