[R] How to speed up the for loop by releasing memeory

Duncan Murdoch murdoch.duncan at gmail.com
Sat Dec 15 17:42:59 CET 2012


On 12-12-15 10:10 AM, Yong Wang wrote:
> Dear list;
>
> How can I speed up the run of following code (illustrative)
> #========================================================================
> con<-vector("numeric")
>
> for (i in 1:limit)
> {
> if(matched data for the ith item found) {
>              if(i==1) {con<-RowOfMatchedData } else
> {con<-rbind(con,matchedData)}
>                                                          }
> }
> #========================================================================
>
> each RowOfMatchedData contains 105 variables, when "i" runs over 10^7
> and the data container "con" get large enough, the codes get extremely
> slow, I know this is a working memory problem (2GB only), is there
> anyway to circumvent this problem without dicing and slicing the data.


You are reallocating and copying con in every step in your loop. 
Preallocate it and just assign new data into the appropriate row and 
things will be much faster.

Duncan Murdoch




More information about the R-help mailing list