title: Using apply functions
body: <p>I have a data frame having these columns</p>
  
  <pre><code>Qty  Sales Price  COGS  
    1          500   200  
    2          900   400   
    3         1000   300   
    1          300   150
  </code></pre>
  
  <p>I want to calculate the profit for every row. So for row 1 if Sales Price - COGS (500-200) profit is 300.
  one condition is that if qty is more than 1 then Qty<em>Sales Prce- Qty</em>COGS= gross profit.</p>
  
  <p>I used loop</p>
  
  <pre><code>for (i in 1:nrow(Data)) {
    if(Data[i,"Qty"]&lt;=1) {Data[i,"Margin"]&lt;-Data[i,"Sales.Price"]-Data[i,"COGS"]}    
    if(Data[i,"Qty"]&gt;=1){Data[i,"Margin"]&lt;-Data[i,"Qty" * "Sales.Price"]-Data[i,"Qty * "COGS"]}
  }
  
  But
  </code></pre>
  
  <p>I want to use apply function</p>
  
  <p>and return my answer in data frame as Margin appended as an vector in my current data frame and not as list or matrix.</p>
  
  <pre><code>Qty    Sales Price    COGS    Margin  
    1            500     200       300  
    2            900     400      1000  
    3           1000     300      2100  
    1            300     150       150  
  </code></pre>