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

Bert Gunter gunter.berton at gene.com
Sun Jul 28 01:53:53 CEST 2013


Perhaps: ?findInterval

(you may need to do some type conversion first)

-- Bert

On Sat, Jul 27, 2013 at 3:28 PM, John Helly <hellyj at ucsd.edu> wrote:
> 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 at 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.
>



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



More information about the R-help mailing list