How can I prevent rbind() from geting really slow as dataframe grows larger?

前端 未结 2 1998
鱼传尺愫
鱼传尺愫 2020-12-01 17:34

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, n         


        
2条回答
  •  时光取名叫无心
    2020-12-01 17:48

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

    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))
    

    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.

提交回复
热议问题