I have large vector with the following type:
myvec <- c(1,2,3,5, 0,1,2,5,8, 0,1,3, 0,2,3,8, 0,3,5)
The corresponding vector for group is:
grp <- c(rep(1, 4), rep(2, 5), rep(3,3), rep(4, 4), rep(5, 3))
I need to add the number such way that the last number of consecutive group will be added to the distance in second group plus 2 (to create a gap). Then last number of second group will added to third pus 3 to create a gap.
thus new vector will look like
myvecnew <- c(1, 2, 3 ,5, # maximum 5 + 2 applied in group 2
5+0+2 = 7, 5+1+2 = 8, 5+2+2 = 9, 5+5+2 = 12, 8+5+2 = 15, # maximum 15 + 2 applied in group 3
0+15+2 = 17, 1+15+2 = 18, 3+15+2 = 20, # maximum 20 + 2 applied to group 4
0 + 20 + 2 = 22,..........and so on)
Thus max(value) of group 1 plus 2 is added to every group 2 values, the resulting new maximum for group 2 will be added to group 3 plus the constant 2, and I need to keep going till all groups are covered.
How can this be achieved? ……..
Reading through what I think are 2 errors in the question (see comments), I believe you can achieve what you want like:
(As you can see, I would also prefer to work with lists)