[R] any way to make the code more efficient ?

Liaw, Andy andy_liaw at merck.com
Fri Dec 8 23:53:50 CET 2006


I don't know about efficiency, but at least for readability, you may
want to do the following:

1. Indent your code.
2. Create a list of appropriate length, and populate the list with
objects you're creating in the loop.
3. After the loop, use do.call(rbind, list).

HTH,
Andy 

From: Leeds, Mark (IED)
> 
> ravi : I appreciate your help but could you be a little more 
> specific about what you mean ? I can just stack aggfxdata 
> below the current full one ( the rbind works out the 
> ordrering by date because it's a zoo object ) so it's not a 
> question of where to put the new one. It's a question of how 
> to avoid rbind ? I apologize because I don't think I 
> understand what you are saying. Or maybe it's not possible to 
> avoid rbind ? Thanks.
> 
> 
> -----Original Message-----
> From: Ravi Varadhan [mailto:rvaradhan at jhmi.edu]
> Sent: Friday, December 08, 2006 5:21 PM
> To: Leeds, Mark (IED); r-help at stat.math.ethz.ch
> Subject: RE: [R] any way to make the code more efficient ?
> 
> 
> Using "rbind" almost always slows things down significantly.  
> You should
> define the objects "aggfxdata" and "fullaggfxdata" before the loop and
> then assign appropriate values to the corresponding rows 
> and/or columns.
> 
> 
> Ravi.
> 
> --------------------------------------------------------------
> ----------
> ----
> -------
> 
> Ravi Varadhan, Ph.D.
> 
> Assistant Professor, The Center on Aging and Health
> 
> Division of Geriatric Medicine and Gerontology 
> 
> Johns Hopkins University
> 
> Ph: (410) 502-2619
> 
> Fax: (410) 614-9625
> 
> Email: rvaradhan at jhmi.edu
> 
> Webpage:
> http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html
> 
>  
> 
> --------------------------------------------------------------
> ----------
> ----
> --------
> 
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Leeds, 
> Mark (IED)
> Sent: Friday, December 08, 2006 4:17 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] any way to make the code more efficient ?
> 
> The code bekow works so this is why I didn't include the data to
> reproduce it. The  loops about 500 times and each time, a zoo object
> with 1400 rows and 4 columns gets created. ( the rows 
> represent minutes
> so each file is one day worth of data). Inside the loop, I 
> keep rbinding
> the newly created zoo object to the current zoo object so that it gets
> bigger and bigger over time.
> 
> Eventually, the new zoo object, fullaggfxdata,  containing 
> all the days
> of data is created.
> 
> I was just wondering if there is a more efficient way of doing this. I
> do know the number of times the loop will be done at the beginning so
> maybe creating the a matrix or data frame at the beginning and putting
> the daily ones in something like that would Make it be 
> faster. But, the
> proboem with this is I eventually do need a zoo object.  I ask this
> question because at around the 250 mark of the loop, things start to
> slow down significiantly and I think I remember reading somewhere that
> doing an rbind of something to itself is not a good idea.  Thanks. 
> 
> #=============================================================
> ==========
> ===============================================
> 
> start<-1
> 
> for (filecounter in (1:length(datafilenames))) { 
> 
> print(paste("File Counter = ", filecounter)) datafile=
> paste(datadir,"/",datafilenames[filecounter],sep="")
> aggfxdata<-clnaggcompcurrencyfile(fxfile=datafile,aggminutes=a
> ggminutes,
> fillholes=1)
> logbidask<-log(aggfxdata[,"bidask"])
> aggfxdata<-cbind(aggfxdata,logbidask)
> 
> if ( start == 1 ) {
> fullaggfxdata<-aggfxdata
> start<-0
> } else {
> fullaggfxdata<-rbind(fullaggfxdata,aggfxdata)
> }
> 
> 
> }
> 
> #=============================================================
> ==========
> ==================================
> --------------------------------------------------------
> 
> This is not an offer (or solicitation of an offer) to
> buy/se...{{dropped}}
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> --------------------------------------------------------
> 
> This is not an offer (or solicitation of an offer) to 
> buy/se...{{dropped}}
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 
> 


------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments,...{{dropped}}




More information about the R-help mailing list