Given is vector:
vec <- c(LETTERS[1:10])
I would like to be able to combine it in a following manner:
resA <- c("AB", "CD", "EF", "GH", "IJ")
resB <- c("ABCDEF","GHIJ")
where elements of the vector vec
are merged together according to the desired size of a new element constituting the resulting vector. This is 2 in case of resA
and 5 in case of resB
.
Desired solution characteristics
- The solution should allow for flexibility with respect to the element sizes, i.e. I may want to have vectors with elements of size 2 or 20
- There may be not enough elements in the vector to match the desired chunk size, in that case last element should be shortened accordingly (as shown)
- This is shouldn't make a difference but the solution should work on words as well
Attempts
Initially, I was thinking of using something on the lines:
c(
paste0(vec[1:2], collapse = ""),
paste0(vec[3:4], collapse = ""),
paste0(vec[5:6], collapse = "")
# ...
)
but this would have to be adapted to jump through the remaining pairs/bigger groups of the vec
and handle last group which often would be of a smaller size.