title: How to create rows using a loop?
body: <p>I have a dataset that looks like this :</p>
  
  <pre><code>ColA; ColB; ColC;
  PAR; BKK; Y;
  BKK; SYD; Y;  
  NYC; LAX; Y;
  LAX; SFO; Y;
  </code></pre>
  
  <p>I want to duplicate the rows where ColC==Y and if colB of a row==colA of another row, then I want to create a row with these values : colA of the first and colB of the second. In our example, it would look like this:</p>
  
  <pre><code>ColA; ColB; ColC;
  PAR; SYD; Y; 
  NYC; SFO; Y;
  </code></pre>
  
  <p>And these rows would be added to the main dataset.</p>
  
  <p>I have tried using a "for" loop, and generating a temporary dataset to rbind the two, but it doesn't work.</p>
  
  <pre><code>for (i in 1:nrow(maindataset)){
      for (j in (i+1):nrow(maindataset)-1){
          if (maindataset$colB[i]==maindataset$colA[j] &amp; maindataset$colC[i]==maindataset$colC[j]) {
          newDF&lt;-data.frame(ColA=maindataset$colA[i],ColB=maindataset$colA[j],ColC=maindataset$colA[j],stringsAsFactors = FALSE)
      maindataset&lt;-rbind(maindataset,newDF)
  }
  }
  }
  </code></pre>
  
  <p>I'm not sure that a loop is the best solution. Do you have any idea of the way I could solve it out?</p>
  
  <p>Thanks!</p>