'correctly appending data to final dataframe of nested for-loop

I have a nested for-loop that I'm having trouble appending data correctly to the final dataframes.

#here is an example data frame

data <- data.frame(BG = c("a", "a", "a", "b", "b", "b", "c", "c", "c","a", "a", "a", "b", "b", "b", "c", "c", "c"), 
                    Herb = c("C","C","C","C","C","C","c","c","c","C","C","C","C","C","C","c","c","c"), 
                    EXTID = c("1","1","1","2","2","2","3","3","3","4","4","4","5","5","5","6","6","6"), 
                    BARCD = c("U1", "U2", "U3", "U4", "U5", "U6", "U7", "U8", "U9", "U10", "U11", "U12", "U13", "U14", "U15", "U16", "U17", "U18"),
                    FL_CT = c("W1_X", "W1_p", "W1_w", "W1_p", "W1_X", "W1_w", "W1_w", "W1_X", "W1_p","W1_X", "W1_p", "W1_w", "W1_p", "W1_X", "W1_w", "W1_w", "W1_X", "W1_p"),
                    PD_CT = c("L_X", "L_T", "L_T", "L_X", "L_T", "L_T", "L_X", "L_T", "L_T", "L_X", "L_T", "L_T", "L_X", "L_T", "L_T", "L_X", "L_T", "L_T"))


here is the loop, note it is only showing 4 selected lines in the C_selections_ALL_BG, where there should be a total of 10. the logic is to remove all the segregating (X's) and put them as discard. There are 18 total lines total. I can't figure out how to set up my dataframes to append the updated information after every iteration until complete. Any help would be appreciated!

P_BG <- unique(data$BG)

C_Selections_ALL_BG <- NULL
C_Discards_ALL_BG <- NULL

for (b in P_BG){
  print(paste0("BG:",b))
  
  # Genotyping results for samples in this Herbicide
  Genotypes_ALL <- data[which(data$BG == b),]
  
  Herb_table<-table(Genotypes_ALL$Herb)
  
  Herbicide_used<-names(which(Herb_table !=0))
  
  C_Selections_ALL_BG_herb <- NULL
  C_Discards_ALL_BG_herb <- NULL
  
  for (h in 1:length(Herbicide_used)){
    
    # Get genotypes in the current platform
    Genotypes<-Genotypes_ALL[which(Genotypes_ALL$Herb == Herbicide_used[h]),]
    
    # get the experiment trial for the herbicide used
    S_EXTID<-unique(Genotypes$EXTID)
    
    C_Selections_EXTID_inBG <- NULL
    C_Discards_EXTID_inBG <- NULL
    
    for (s in S_EXTID){
      
      C_Selections <- NULL
      C_Discards <- NULL
      
      print(s)
      
      # Select the lines in this S_EXTID
      Geno_lines<-Genotypes[which(Genotypes$EXTID == as.character(s)),]
      
      #if lines are Conv
      if (Herbicide_used[h] == "C"){
        
        Conv_Geno_lines <- Geno_lines
        
        #Remove hets for flower color
        if (length(intersect(colnames(Conv_Geno_lines),c("FL_CT")))==1){
          
          #identify which are hets and create discard df for adding discard reason
          C_FC_het <- Conv_Geno_lines[which(grepl("^.+(_X)$",Conv_Geno_lines$FL_CT)),]
          C_FC_discard <- C_FC_het[,c("BARCD", "FL_CT")]
          colnames(C_FC_discard) <- c("BARCD", "Discard_Reason")
          
          #remove lines with flow color hets
          if (length(C_FC_discard$BARCD)>0){
            Conv_Geno_lines <- Conv_Geno_lines[-which(grepl("^.+(_X)$",Conv_Geno_lines$FL_CT)),]
          }
        }
        
        #Remove hets for flower color
        if (length(intersect(colnames(Conv_Geno_lines),c("PD_CT")))==1){
          
          #identify which are hets and create discard df for adding discard reason
          C_PD_het <- Conv_Geno_lines[which(grepl("^.+(_X)$",Conv_Geno_lines$PD_CT)),]
          C_PD_discard <- C_PD_het[,c("BARCD", "PD_CT")]
          colnames(C_PD_discard) <- c("BARCD", "Discard_Reason")
          
          #remove lines with flow color hets
          if (length(C_PD_discard$BARCD)>0){
            Conv_Geno_lines <- Conv_Geno_lines[-which(grepl("^.+(_X)$",Conv_Geno_lines$FL_CT)),]
          }
        }
        
      #append conv selections and discards
      C_Selections <- Conv_Geno_lines
      C_Discards <- rbind(C_FC_discard, C_PD_discard)
      }
    C_Selections_EXTID_inBG <- rbind(C_Selections_EXTID_inBG, C_Selections)
    C_Discards_EXTID_inBG <- rbind(C_Discards_EXTID_inBG, C_Discards)
    }
  C_Selections_ALL_BG_herb <- rbind(C_Selections_ALL_BG_herb, C_Selections_EXTID_inBG)
  C_Discards_ALL_BG_herb <- rbind(C_Discards_ALL_BG_herb, C_Discards_EXTID_inBG)
  }
C_Selections_ALL_BG <- rbind(C_Selections_ALL_BG, C_Selections_ALL_BG_herb)
C_Discards_ALL_BG <- rbind(C_Discards_ALL_BG, C_Discards_ALL_BG_herb)
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source