I am struggling to subset a data set according to one continous variable - let's call it "Time" - starting from 1 and incrementing by 1 each line (1, 2, 3, 4, 5, ..) but sometimes I get some missing values so I get some "jumps" (ex. 1, 2, 3, 4, 5, 6, 10, 11, 12, 20, 21, 22, 23). These jumps are random so they don't get the same number of missing values in between. I want to split the dataset in multiples datasets when these "jumps" occur, so in my exemple it would give one data set from 1 to 6, another from 10 to 12, and another from 20 to 23. Any idea how to do that ? That would be a great help...
We can use group_split
after defining the consecutive groups with cumsum(c(1, diff(Time) != 1))
:
library(dplyr)
library(purrr)
library(tibble)
tibble(Time = c(1, 2, 3, 4, 5, 6, 10, 11, 12, 20, 21, 22, 23)) %>%
group_split(group = cumsum(c(1, diff(Time) != 1))) %>%
map(~ select(., -group))
[[1]]
# A tibble: 6 × 1
Time
<dbl>
1 1
2 2
3 3
4 4
5 5
6 6
[[2]]
# A tibble: 3 × 1
Time
<dbl>
1 10
2 11
3 12
[[3]]
# A tibble: 4 × 1
Time
<dbl>
1 20
2 21
3 22
4 23