我怎样才能prevent rbind()从歌厅的数据框变大很慢? [英] How can I prevent rbind() from geting really slow as dataframe grows larger?

查看:158
本文介绍了我怎样才能prevent rbind()从歌厅的数据框变大很慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只有1行中的数据帧。对此我开始使用rbind添加行

I have a dataframe with only 1 row. To this I start to add rows by using rbind

df #mydataframe with only one row
for (i in 1:20000)
{
    df<- rbind(df, newrow)

}

这得到,因为我生长非常缓慢。这是为什么?我怎么能做出这种类型的code的更快?

this gets very slow as i grows. Why is that? and how can I make this type of code faster?

推荐答案

您在的第二圈地狱,即未能pre-分配的数据结构。

You are in the 2nd circle of hell, namely failing to pre-allocate data structures.

在这种方式成长的对象是在一个非常非常糟糕的事情R.无论是pre-分配并插入:

Growing objects in this fashion is a Very Very Bad Thing in R. Either pre-allocate and insert:

df <- data.frame(x = rep(NA,20000),y = rep(NA,20000))

或调整你的code,以避免这种增量加入行组成。截至我举的链接讨论的,缓慢的原因是,每次添加一行时,R需要找到一个新的内存连续块以适合。很多'O复制的数据帧。

or restructure your code to avoid this sort of incremental addition of rows. As discussed at the link I cite, the reason for the slowness is that each time you add a row, R needs to find a new contiguous block of memory to fit the data frame in. Lots 'o copying.

这篇关于我怎样才能prevent rbind()从歌厅的数据框变大很慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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