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

William Dunlap wdunlap at tibco.com
Mon Jul 29 16:22:16 CEST 2013


Look into the findInterval function.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of John Helly
> Sent: Saturday, July 27, 2013 3:29 PM
> To: r-help at 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



More information about the R-help mailing list