title: Streamlining R code, double for loop
body: <p>I have a rather large dataset that I've read into R and formatted the way I would like.  I have 3 columns, latitude, longitude, and an indicator for whether or not there is snow at that location.</p>
  
  <p>I also have another (larger) dataset with 3 columns: latitude, longitude, and elevation.  I would like to sort through these latitudes and longitudes and match them up with the lats and longs in my other dataset to add in the elevation.  Essentially I would like 1 dataset with 4 columns: lat, long, indicator, and elevation.</p>
  
  <p>I have written a program which does this, but for these very large datasets, it is not efficient enough to let it run.  I do not want to kill my personal PC.  Is there any way to speed up my code?</p>
  
  <p>Thanks so much.</p>
  
  <pre><code>      ## Looping through
        for(i in 1:len){
          for(j in 1:78355){
            if(OrigDat1[i,2] == Elev04[j,2] &amp;&amp; OrigDat1[i,1] == Elev04[j,1]){
              Elev.vec[i] = as.numeric(Elev04[j,3])
            }
          }
        }
  </code></pre>
  
  <p>In the above code the 'len' term varies depending on the size of the shapefile I strip the data down to.  It has a length anywhere from 1000 - 50000 locations.</p>
  
  <p>I can provide you with more information if needed.</p>
  
  <p>Thanks again.</p>