group () from groupdata2 can create groups from the list of starting points of groups using the l_starts method. By setting n to auto , it automatically finds the start of the group:
x <- c(1,1,1,2,2,2,3,2,2,1,1) groupdata2::group(x, n = "auto", method = "l_starts") ## # A tibble: 11 x 2 ## # Groups: .groups [5] ## data .groups ## <dbl> <fct> ## 1 1 1 ## 2 1 1 ## 3 1 1 ## 4 2 2 ## 5 2 2 ## 6 2 2 ## 7 3 3 ## 8 2 4 ## 9 2 4 ## 10 1 5 ## 11 1 5
There is also a differs_from_previous() function that finds values ββor indices of values ββthat differ from the previous value in some threshold values.
# The values to start groups at differs_from_previous(x, threshold = 1, direction = "both")
source share