Here I have a numeric vector and I would like to add val_to_add to each element and append those extra values in sample_vec, with a ceiling (max_val).
set.seed(53)
max_val = 50
val_to_add = 2
sample_vec <- sort(sample(1:max_val, 8))
[1] 3 5 6 15 29 30 35 50
For example, I want to add 2 to each element in sample_vec, so for the first element, it should be 3:(3 + 2), which is 3 4 5.
Duplicated values should be discarded and the maximum value in this case should be 50. The desired output is something like this:
[1] 3 4 5 6 7 8 15 16 17 29 30 31 32 35 36 37 50
Here's my current code:
out_vec <- unique(c(sapply(sample_vec, function(x) sequence(val_to_add + 1, from = x))))
out_vec[out_vec <= max_val]
[1] 3 4 5 6 7 8 15 16 17 29 30 31 32 35 36 37 50
Is there any existing function for this kind of operation maybe in base R?