title: How to do the same thing in R that we do using CROSS APPLY in SQL Server?
body: <p>that is how to run a condition for each row in the outer data frame for each row in the inner data frame) I have two data frames : </p>
  
  <p><strong>ToTest</strong></p>
  
  <pre><code>   Origin.State Dest.State  Ship.Date Cost
  1            IL         NY 2015-03-25   10
  2            IL         NY 2015-03-25   10
  3            IL         NY 2015-03-24   10
  4            IL         NY 2015-03-23   10
  5            IL         NY 2015-03-18   10
  6            PA         NY 2015-04-29   10
  7            PA         NY 2015-04-29   10
  8            PA         NY 2015-04-27   10
  9            PA         NY 2015-04-24   10
  10           PA         NY 2015-03-01   10
  11           IL         TX 2015-05-18   10
  12           IL         TX 2015-05-18   10
  13           IL         TX 2015-05-14   10
  14           IL         TX 2015-05-12   10
  15           IL         TX 2015-05-13   10
  </code></pre>
  
  <p><strong>TestShipmentGroup1</strong></p>
  
  <pre><code>   Origin.State Dest.State  Ship.Date
  1            IL         NY 2015-03-25
  2            IL         NY 2015-03-24
  3            IL         NY 2015-03-23
  4            IL         NY 2015-03-18
  5            PA         NY 2015-04-29
  6            PA         NY 2015-04-27
  7            PA         NY 2015-04-24
  8            PA         NY 2015-03-01
  9            IL         TX 2015-05-18
  10           IL         TX 2015-05-14
  11           IL         TX 2015-05-12
  12           IL         TX 2015-05-13
  </code></pre>
  
  <p>I am trying to apply the conditions shown below to every row in the <strong>ToTest</strong> dataframe using each row of <strong>TestShipmentGroup1</strong> dataframe at a time.</p>
  
  <pre><code>for (i in 1: nrow(TestShipmentGroup1))
  {
  TestShipmentGroup1%&gt;%
    select(Origin.State,Dest.State,Ship.Date)
  ToTest%&gt;%
    select(Origin.State, Dest.State,Ship.Date,Cost) %&gt;% 
    filter (((ToTest$Ship.Date &gt;= (TestShipmentGroup1$Ship.Date-7)) 
             &amp; (ToTest$Ship.Date &lt; TestShipmentGroup1$Ship.Date))
            &amp; (ToTest$Origin.State == TestShipmentGroup1$Origin.State)
            &amp; (ToTest$Dest.State == TestShipmentGroup1$Dest.State))}
  </code></pre>