title: Fetch value from previous row and concatenate if condition is met in R
body: <p>I want to fetch string from previous row and concatenate with current row string if condition is met from another column</p>
  
  <p><strong>Condition</strong>: If 'Flag' is 1 then concatenate after fetching string from previous row in column AG and the current row string from columns AG.</p>
  
  <p>My data frame sample is given below this and the desired result is given in "Desired result" column</p>
  
  <pre><code>AG       Flag   Desired result
  Team a   0  
  Team b   0  
  CI       0  
  Team a   1      CI to Team a
  Team b   0  
  Team b   0  
  C        0  
  CI       1      C to CI
  Team a   0  
  Team b   1      Team a to Team b
  </code></pre>
  
  <p>and my code is </p>
  
  <pre><code>mydata$trans &lt;- ifelse(mydata$Flag == 1,paste(mydata$AG[-1],"to",mydata$AG[-nrow(mydata)]),"")
  </code></pre>
  
  <p>I got the following result:  </p>
  
  <pre><code>AG       Flag   trans
  Team a   0  
  Team b   0  
  CI       0  
  Team a   1      Team b to Team a
  Team b   0  
  Team b   0  
  C        0  
  CI       1      Team a to CI
  Team a   0  
  Team b   0 
  C        0      
  CI       1      Team a to CI
  Team a   0      
  Team b   1      Team b to Team a
  </code></pre>
  
  <p>I realised my code is wrong but I'm not able to find out solution even after many trial and error.
  Thanks in advance for your help !</p>