Given a strictly increasing vector (each element is bigger than the last), group together stretches of the vector where adjacent elements are separated by at most some specified distance. Hence, each element in each group has at least one other element in that group that is close to it. See the examples.

group_close(vec_ascending, max_gap = 1)

Arguments

vec_ascending

A strictly increasing numeric vector.

max_gap

The biggest allowable gap between adjacent elements for them to be considered part of the same group.

Value

A where each element is one group, as a numeric vector.

Examples

group_close(1:10, 1)
#> [[1]]
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> 
group_close(1:10, 0.5)
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 2
#> 
#> [[3]]
#> [1] 3
#> 
#> [[4]]
#> [1] 4
#> 
#> [[5]]
#> [1] 5
#> 
#> [[6]]
#> [1] 6
#> 
#> [[7]]
#> [1] 7
#> 
#> [[8]]
#> [1] 8
#> 
#> [[9]]
#> [1] 9
#> 
#> [[10]]
#> [1] 10
#> 
group_close(c(1, 2, 4, 10, 11, 14, 20, 25, 27), 3)
#> [[1]]
#> [1] 1 2 4
#> 
#> [[2]]
#> [1] 10 11 14
#> 
#> [[3]]
#> [1] 20
#> 
#> [[4]]
#> [1] 25 27
#>