Consider a monotonically increasing integer sequence such as:
x <- c(0, 3, 5, 8, 10, 16, 18, 35, 36)
I would like to group these based on their difference from each other. If the difference is less than or equal to 4
I would like them to be in the same group -- however that difference needs to reset once a group is assigned.
# x desired_group
# 1 0 0
# 2 3 0
# 3 5 1
# 4 8 1
# 5 10 2
# 6 16 3
# 7 18 3
# 8 35 4
# 9 36 4
{0, 3} go together because they are within 4. Once we reach 5, that grouping needs to reset. That is, floor(x / 4)
will not work because it does not "reset" appropriately.