Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

How to create rows using a loop?

$
0
0

I have a dataset that looks like this :

ColA; ColB; ColC;
PAR; BKK; Y;
BKK; SYD; Y;  
NYC; LAX; Y;
LAX; SFO; Y;

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:

ColA; ColB; ColC;
PAR; SYD; Y; 
NYC; SFO; Y;

And these rows would be added to the main dataset.

I have tried using a "for" loop, and generating a temporary dataset to rbind the two, but it doesn't work.

for (i in 1:nrow(maindataset)){
    for (j in (i+1):nrow(maindataset)-1){
        if (maindataset$colB[i]==maindataset$colA[j] & maindataset$colC[i]==maindataset$colC[j]) {
        newDF<-data.frame(ColA=maindataset$colA[i],ColB=maindataset$colA[j],ColC=maindataset$colA[j],stringsAsFactors = FALSE)
    maindataset<-rbind(maindataset,newDF)
}
}
}

I'm not sure that a loop is the best solution. Do you have any idea of the way I could solve it out?

Thanks!


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>