[R] Resources for optimizing code
    Janet Elise Rosenbaum 
    jerosenb at fas.harvard.edu
       
    Fri Nov  5 18:34:55 CET 2004
    
    
  
I want to eliminate certain observations in a large dataframe (21000x100).
I have written code which does this using a binary vector (0=delete obs,
1=keep), but it uses for loops, and so it's slow and in the extreme it 
causes R to hang for indefinite time periods.
I'm looking for one of two things:
1.  A document which discusses how to avoid for loops and situations in
which it's impossible to avoid for loops.
or
2.  A function which can do the above better than mine.  
My code is pasted below.
Thanks so much,
Janet 
# asst is a binary vector of length= nrow(DATAFRAME).  
# 1= observations you want to keep.  0= observation to get rid of.
remove.xtra.f <-function(asst, DATAFRAME) {
	n<-sum(asst, na.rm=T)
	newdata<-matrix(nrow=n, ncol=ncol(DATAFRAME))
	j<-1
	for(i in 1:length(data)) {
		if (asst[i]==1) {
			newdata[j,]<-DATAFRAME[i,]
			j<-j+1
		}
	}
	newdata.f<-as.data.frame(newdata)
	names(newdata.f)<-names(DATAFRAME)
	return(newdata.f)
}
--  
Janet Rosenbaum                                 jerosenb at fas.harvard.edu
PhD Candidate in Health Policy, Harvard GSAS
Harvard Injury Control Research Center, Harvard School of Public Health
    
    
More information about the R-help
mailing list