I want to make a loop in R that as a result saves a range whose minimum size is 1000 (initial binLength) and increases its size by 10 points (stepSize) each time the condition is not met. I send the script that I have but I am not getting the result I want.
set.seed(123)
newbins = seq(0,5000,1000)
binLength = 1000
stepSize = 10
blocks=NULL
end = 0
for (b in newbins[1:length(newbins)-1]){
tempBP = 0
tempBP_new = 0
start = end +1
end = binLength
while (sum(tempBP_new-tempBP) < 0.40){
tempBP_new = tempBP
tempBP_new = runif(1)*0.5
binLength = binLength + stepSize
blocks = rbind(blocks,c(start, end))
}
}
blocks
The output of the script is:
[,1] [,2]
[1,] 1 1000
[2,] 1 1000
[3,] 1 1000
[4,] 1 1000
[5,] 1001 1040
[6,] 1041 1050
[7,] 1041 1050
[8,] 1041 1050
[9,] 1051 1080
[10,] 1051 1080
[11,] 1051 1080
[12,] 1081 1110
[13,] 1081 1110
[14,] 1081 1110
[15,] 1081 1110
[16,] 1081 1110
But I want something like for example
1 1000
1001 2040
2041 3050
3050 5000
In other words, each range must be at least 1000 and the loop has to end when it reaches 5000. I also don't understand why I have the same result several times in the loop. Do you have any ideas? Thanks a lot!