I have the following data frame in R:
gene_name gene_number
ENSMUSG00000000001 4732
ENSMUSG00000000001 4733
ENSMUSG00000000058 7603
ENSMUSG00000000058 7604
ENSMUSG00000000058 8246
ENSMUSG00000000058 8248
ENSMUSG00000000058 9001
The data is grouped by gene_name column, and the gene_number is sorted by other parameters (not relevant for the question). I want to sub-group the data according to the gene_number. Inside each group, I want to sub group the data if the values in gene_number are not sequential / consecutive or the maximum differences between following rows is 2. If there is only 1 value without sequential value, I would like to remover it.
I want to have a new column specifying the new groups.
For example, in the data above:
ENSMUSG00000000001 4732 1
ENSMUSG00000000001 4733 1
ENSMUSG00000000058 7603 2
ENSMUSG00000000058 7604 2
ENSMUSG00000000058 8246 3
ENSMUSG00000000058 8248 3
Thank you!