Look into the findInterval function. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com
> -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf > Of John Helly > Sent: Saturday, July 27, 2013 3:29 PM > To: r-help@r-project.org > Subject: [R] Alternative method for range-matching within 2 nested loops in R? > > Hi. > > 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 ______________________________________________ R-help@r-project.org 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.