0

我在一个循环中生成一些变量,稍后我将它们一次一行存储到数据帧中。我觉得我使用了太多的 cbind 和 rbind ,这使得代码效率低下。以下结构的替代方案是什么。

Sys.time()
outData = c()
for (i in 1:40000)
{
  a=0; b=0; c=0;d=0;e=0;f=0;g=0
  #newline = cbind(a,cbind(b,cbind(c,cbind(d,cbind(e,f)))))
  newline = do.call(cbind, list(a,b,c,d,e,f,g))
  outData = rbind(outData, newline)
}
Sys.time()

编辑: do.call() 在这里似乎更慢。

4

1 回答 1

1

EDIT Based on the new code in the OP:

You dont even need cbind. just whatever you are assigning to a, b, c, d... pyt that right into a data.frame. or at the very worst

newline=data.frame(a,b,c,d...,etc) 



The inefficiencies are most likely coming from generating i==1000 single row data.frames one at a time. There is likely a much more effective solution.

With regards to the complex cbind line, try the following instead:

    #instead of:
    newline = cbind(a,cbind(b,cbind(.......z,cbind(a1,a2))))))

    # try: 
    newline = do.call(cbind, list(a, b, ..., z))
于 2013-08-02T18:32:36.690 回答