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

Ravi Varadhan rvaradhan at jhmi.edu
Fri Dec 8 23:20:37 CET 2006


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=aggminutes,
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.




More information about the R-help mailing list