John Helly
2013-Jul-27 22:28 UTC
[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
Bert Gunter
2013-Jul-27 23:53 UTC
[R] Alternative method for range-matching within 2 nested loops in R?
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
William Dunlap
2013-Jul-29 14:22 UTC
[R] Alternative method for range-matching within 2 nested loops in R?
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