Say I have the following vector:
c(4, 5, 5, 8, 12, 12, 12, 13, 15, 15, 18, 19, 20, 23, 37, 37, 37, 37, 37, 41)
and I would like to βgroupβ its elements according to their meaning: numbers that differ <= 3 should be considered to belong to the same group. In this case, I would like every number that appears in the vector to get all the numbers close to it. For instance,
4 --> c(4,5,5,8) 5 --> c(4,5,5,8) 8 --> c(5,8) 12 --> c(12,12,12,13,15,15)
etc.
Perhaps it would be useful to get their index as well ... Is there any smart way to achieve this?