有效地向R中的向量或列表添加或删除元素? [英] Efficiently adding or removing elements to a vector or list in R?

查看:49
本文介绍了有效地向R中的向量或列表添加或删除元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施一种算法,该算法涉及从集合中添加和删除大量内容.在 R 中,这很慢,因为据我所知,从向量中添加或删除内容很慢,因为必须重新分配整个向量.有没有更有效的方法?

I'm implementing an algorithm that involves lots of adding and removing things from sets. In R, this is slow because as far as I know, adding or removing things from a vector is slow, since the entire vector has to be re-allocated. Is there a way do do it more efficiently?

我当前的解决方案是使用与集合中的事物列表长度相同的布尔向量,并将其用作成员资格表.

My current solution is to use a boolean vector of the same length as the list of things that can be in the set, and using that as a membership table.

推荐答案

R Inferno 对此有一些有趣的评论,包括周期性增长的对象以减少内存碎片和分配开销.

Chapter 2 of The R Inferno has some interesting comments on this, including perdiodic growing objects to reduce memory fragmentation and allocation overhead.

如果您知道集合的最终大小是多少,那么您建议的方法可能是最好的 - 即使用适当的成员向量从整个宇宙中subset.但是,如果不确切了解您正在尝试做什么,就很难知道什么是最好的.

If you know what the ultimate size of the set is, then the method you suggest is probably the best - ie subset from the whole universe using an approprate membership vector. Difficult to know whats best without seeing exactly what you are trying to do though.

这篇关于有效地向R中的向量或列表添加或删除元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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