Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First look at these general suggestions on speeding up code: <a href="https://stackoverflow.com/a/8474941/636656">https://stackoverflow.com/a/8474941/636656</a></p> <p>The first thing that jumps out at me is that I'd create only one results matrix. That way you're not duplicating the <code>sampleIDs begin finish</code> columns, and you can avoid any overhead that comes with running the matching algorithm twice.</p> <p>Doing that, you can avoid selecting more than once (although it's trivial in terms of speed as long as you store your selection vector rather than re-calculate).</p> <p>Here's a solution using <code>apply</code>:</p> <pre><code>myMatrix1 &lt;- data.frame(sampleIDs=c(19990224,20000224),begin=c(4,5),finish=c(5,6)) myMatrix2 &lt;- data.frame(begin=c(0,11),finish=c(10,367),sed=c(1.002,2.01),boreWidth=c(.014,.056)) glommer &lt;- function(x,myMatrix2) { x[4:5] &lt;- as.numeric(myMatrix2[ myMatrix2$begin &lt;= x["begin"] &amp; myMatrix2$finish &gt;= x["finish"], c("sed","boreWidth") ]) names(x)[4:5] &lt;- c("sed","boreWidth") return( x ) } &gt; t(apply( myMatrix1, 1, glommer, myMatrix2=myMatrix2)) sampleIDs begin finish sed boreWidth [1,] 19990224 4 5 1.002 0.014 [2,] 20000224 5 6 1.002 0.014 </code></pre> <p>I used <code>apply</code> and stored everything as numeric. Other approaches would be to return a data.frame and have the sampleIDs and begin, finish be ints. That might avoid some problems with floating point error.</p> <p>This solution assumes there are no boundary cases (e.g. the begin, finish times of myMatrix1 are entirely contained within the begin, finish times of the other). If your data is more complicated, just change the glommer() function. How you want to handle that is a substantive question.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload