There might be a simple solution to this but I'm struggling.
I have a code as follows:
for(i in 1:nrow(df)){
x[i] <- df[i,]$X4
if(length(unique(df[1:i,]$X4)) == length(unique(df$X4))){
break
}
collect <- data.frame(df[1,]$X1, df[1,]$X2, df[i+1,]$X3)
}
The loop breaks after the if condition length(unique(df[1:i,]$X4)) == length(unique(df$X4)) is reached. However, I want to start the same loop again from i+1'th iteration, and keep checking until the same if condition is met again, till the end of my dataframe.
My sample data is as follows:
1       930000  1000000 E2-A
1       1890000 2110000 E2-A
1       2120000 2330000 D
1       2340000 3350000 E2-B
1       3365000 3405000 B
1       5695000 5810000 E2-A
1       6305000 6405000 E2-B
1       6425000 6465000 E1-A
1       6780000 6960000 E2-B
1       7100000 7270000 D
1       7730000 7810000 D 
1       8030000 8380000 E2-A
1       8970000 9170000 E1-A
1       9345000 9555000 E1-B
1       9845000 9930000 E1-A
1       10000000        10100000        E1-B
1       10430000        10560000        E3
1       11720000        11780000        B
1       11900000        11960000        C
1       12185000        12270000        E1-A
1       12450000        12680000        A  #break point of loop because if(length(unique(df[1:i,]$X4)) == length(unique(df$X4)))
1       13990000        14290000        B
1       15250000        15355000        E2-B
1       15475000        15600000        D
1       15655000        15755000        A
1       15920000        16080000        E2-A
1       16120000        16280000        C
1       16400000        16570000        E1-B
1       17280000        17380000        E1-B
1       17450000        17735000        A
1       17760000        17820000        E1-B
1       17825000        17935000        A
1       18925000        19150000        E1-A
1       19220000        19410000        C
1       19680000        19980000        C
1       20230000        20820000        E3 #the if condition is met again after the break, but using break exits the loop
1       20845000        20970000        E2-A
1       21580000        21695000        D
1       21700000        21920000        E2-A
1       22430000        22750000        B
1       22740000        22980000        A
1       23300000        23515000        C
1       23870000        23965000        A
1       24525000        24720000        E2-B
1       25010000        25160000        D
1       25170000        25430000        B
1       25930000        26130000        A
1       26220000        26330000        E2-B
1       26435000        26485000        C
My expected output is:
1       930000        12680000        
1       13990000      20820000
But what I get so far is:
1       930000        12680000        
How do I do so?
 
    