I have the following vector of indices
V_ind = cumsum(c(10,9,8,7,6,5,4,3,2,1))
[1] 10 19 27 34 40 45 49 52 54 55
And I created the following FOR LOOP
k=1
for(ind in V_ind){
if(ind<=10){
print("ok")
}else{
print(c(V_ind[1:k]))
k = k + 1
}
}
Which gives as a result
[1] "ok"
[1] 10
[1] 10 19
[1] 10 19 27
[1] 10 19 27 34
[1] 10 19 27 34 40
[1] 10 19 27 34 40 45
[1] 10 19 27 34 40 45 49
[1] 10 19 27 34 40 45 49 52
[1] 10 19 27 34 40 45 49 52 54
However, what I try to acheive is the following result
[1] "ok"
[1] 10
[1] 9 10 19
[1] 8 9 10 18 19 27
[1] 7 8 9 10 17 18 19 26 27 34
[1] 6 7 8 9 10 16 17 18 19 25 26 27 33 34 40
[1] 5 6 7 8 9 10 15 16 17 18 19 24 25 26 27 32 33 34 39 40 45
[1] 4 5 6 7 8 9 10 14 15 16 17 18 19 23 24 25 26 27 31 32 33 34 38 39 40 44 45 49
[1] 3 4 5 6 7 8 9 10 13 14 15 16 17 18 19 22 23 24 25 26 27 30 31 32 33 34 37 38 39 40 43 44 45 48 49 52
[1] 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 21 22 23 24 25 26 27 29 30 31 32 33 34 36 37 38 39 40 42 43 44 45 47 48 49 51 52 54
This result goes as follows:
In the first iteration, we just print OK
In the second iteration, we extract the first element of the vector
V_ind,In the third iteration, we extract the first and second element of the vector
V_indtogether with the first element of the vectorV_indminus 1 that is the number9.In the fourth iteration, we extract the first, second and third element of the vector
V_ind, together with the first element minus 1, i.e.9, first element minus 2, i.e8, and second element minus 1,i.e.18.In the fifth iteration, we extract the first, second, third and fourth element of the vector
V_indtogether with the first element minus 1, 2, 3 respectively, i.e7,8,9, also the second element minus 1 and 2, i.e17,18, and the third element minus 1, i.e26.
And this procedure goes until the end of the FOR LOOP. Is this even possible to be done in R, in a generic way?