title: Compare two columns of Unequal Length in R using logical Operator
body: <p>I am dealing with a big time series dataset, And I would like to compare two columns
  So my first Column looks like </p>
  
  <pre><code>            timeperiod          timefortreatment
         2014-08-01 00:00:00        102.81818
         2014-08-01 01:00:00         12.34483
         2014-08-01 02:00:00         35.67568
         2014-08-01 03:00:00        125.57692
         2014-08-01 04:00:00         97.56250
         2014-08-01 05:00:00         36.66667
  </code></pre>
  
  <p>And the second column looks like</p>
  
  <pre><code>        arrivaltime         
         2014-08-01 00:14:00       
         2014-08-01 00:22:00         
         2014-08-01 00:47:00         
         2014-08-01 01:07:00        
         2014-08-01 01:19:00         
         2014-08-01 01:53:00 
  </code></pre>
  
  <p>Both are of unequal lengths with second being larger than first. I have to compare the first column with second to get a final one which looks like below. The logic for comparison is that if arrival time  in the second column is less than the entry in first column (time  being 1 hour here) it gets the value of time of treatment for that specific  period</p>
  
  <pre><code>             arrival          timefortreatment
         2014-08-01 00:14:00        102.81818
         2014-08-01 00:22:00        102.81818
         2014-08-01 00:47:00        102.81818
         2014-08-01 01:07:00         12.34483
         2014-08-01 01:19:00         12.34483
         2014-08-01 01:53:00         12.34483
  </code></pre>
  
  <p>I have made a logic based on two <code>for</code> loops and it is taking forever for 50k + values:</p>
  
  <pre><code>for (i in 1:nrow(date)) 
  {
      for (j in 1:nrow(period))
      { 
          if (date[i,1]&gt;=period[j,])
          { 
              z[i,]=t[j,] 
              j=j+1
          } 
      } 
      i=i+1 
  }
  </code></pre>
  
  <p>I was wondering is there any other way in which this can be done. Any help in this regard will be highly appreciated. Editing my answer to accommodate for the cases with different time  period.    </p>
  
  <pre><code>             timeperiod                  timefortreatment
                2014-08-01 00:14:00               75
                2014-08-01 00:19:00              143
                2014-08-01 00:44:00              126
                2014-08-01 01:04:00              125
                2014-08-01 01:19:00              125
                2014-08-01 01:49:00              122
  </code></pre>
  
  <p>For this case, output will be as shown below based  on same  logic i.e. (arrival>=time period)</p>
  
  <pre><code>              arrival          timefortreatment
         2014-08-01 00:14:00            75
         2014-08-01 00:22:00           143
         2014-08-01 00:47:00           126
         2014-08-01 01:07:00           125
         2014-08-01 01:19:00           125
         2014-08-01 01:53:00           122 
  </code></pre>
  
  <p>Let me know if more details needed</p>