R中的预分配列表 [英] preallocate list in R

查看:23
本文介绍了R中的预分配列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 R 中循环扩展数据结构是低效的.如何预分配特定大小的 list?matrix 通过 ncolnrow 参数使这变得容易.如何在列表中做到这一点?例如:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆