After staring at my solution a little longer, I now think that
cum_auc isn't the value you wanted, right? The solution I gave in the last comment is the cumulative area under the function starting at zero and adding "trapezoids" to the right. But you wanted the area from time t to the last time (i.e. start adding trapezoids to the right from time t). I think this solution is now correct:
hoist(data, time = 'time', glucose = 'glucose', .remove = FALSE) %>%
mutate(cum_auc = map2(time, glucose, ~cumtrapz(.x, .y)[,1])) %>%
unnest(c(time, cum_auc)) %>%
mutate(cum_auc = max(cum_auc) - cum_auc) %>%
Check it out and let me know what you think.
Regarding the gaps in signal. How would you like to handle this? Should it be linearly interpolated? Some other interpolation approach? There are definitely ways to handle gaps in data, but you'll have to think about what is appropriate for your data.