I am generating some variables in a loop which I later store into a dataframe one line at a time. I feel that I am using too much of cbind and rbind which is making the code inefficient. What is an alternative to the following structure.
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()
Edit: do.call() seems to be slower here.
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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.