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!