'Episode extraction

I totally lost on this. I am using time-use data I would like to extract episodes based on time steps. So basically, individuals are asked to take 3 measurements at the same time denoted by 3 variables a_1, b_1 and c_1. In some cases, they reported the same measurement at a_2, b_2 and c_2. I would like to extract the length (the minimum length is 2) of the same measurements based on time-steps the frequency of the occurrence as well the start and end times.

For example, below the highlighted red and blue cells denote same measurement based on time-steps and not per id.

enter image description here

enter image description here

Possible output:

![enter image description here

Sample data:

structure(list(a_1 = c(100, 100, NA), a_2 = c(101, 101, NA), 
    a_3 = c(100, 100, NA), a_4 = c(1234, 1234, NA), b_1 = c(4567, 
    100, NA), b_2 = c(101, 101, NA), b_3 = c(100, 100, NA), b_4 = c(1234, 
    1234, NA), c_1 = c(3456, 100, NA), c_2 = c(101, 101, NA), 
    c_3 = c(100, 100, NA), c_4 = c(1234, 1234, NA)), spec = structure(list(
    cols = list(a_1 = structure(list(), class = c("collector_double", 
    "collector")), a_2 = structure(list(), class = c("collector_double", 
    "collector")), a_3 = structure(list(), class = c("collector_double", 
    "collector")), a_4 = structure(list(), class = c("collector_double", 
    "collector")), b_1 = structure(list(), class = c("collector_double", 
    "collector")), b_2 = structure(list(), class = c("collector_double", 
    "collector")), b_3 = structure(list(), class = c("collector_double", 
    "collector")), b_4 = structure(list(), class = c("collector_double", 
    "collector")), c_1 = structure(list(), class = c("collector_double", 
    "collector")), c_2 = structure(list(), class = c("collector_double", 
    "collector")), c_3 = structure(list(), class = c("collector_double", 
    "collector")), c_4 = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), delim = ","), class = "col_spec"), row.names = c(NA, 
-3L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"))


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source