[R] Alternative method for range-matching within 2 nested loops in R?

John Helly hellyj at ucsd.edu
Sun Jul 28 00:28:42 CEST 2013


I've been puzzling about how to replace the nested loops below. The idea 
is that the B dataframe has rows with a posix datetime and the C 
dataframes has posix Start and End times.  I want to assign a value to 
the observations in B based in intersecting the appropriate 
time-interval in C.  I haven't been able to discern a more efficient way 
to do this.  Any suggestions would be most appreciated.

brows = dim(B)[1]
mrows = dim(C)[1]

for (i in 1:brows ) {
     for (j in 1:mrows ) {
         if (B$Datetime[i] >= C$DT_Start[j] & B$Datetime<=C$DT_End[j]){
             B$Site[i] = C$Proximity[j]

John Helly, University of California, San Diego / San Diego Supercomputer Center / Scripps Institution of Oceanography / 760 840 8660 mobile / stonesteps (Skype) / stonesteps7 (iChat) / http://www.sdsc.edu/~hellyj

More information about the R-help mailing list