Another base R method:
indx <- !(1:nrow(dat) %in% unlist(lapply(indexes, '[', -1))) transform(dat, y=ave(y, cumsum(indx), FUN=toString))[indx,]
Explanation
Some understanding of how !(1:nrow(dat) %in% unlist(lapply(indexes, '[', -1))) happened:
I tried to find an index for grouping. I started from the end and worked back. I knew that if I could:
1 2 2 3 4 5 5 5 6 7 8 8 9
I could use ave and run toString . I thought it was necessary to use a true and false combination to make cumsum above metric. I wrote this:
cumsum(c(T, T, F, T, T, T, F, F, T, T, T, F, T)) [1] 1 2 2 3 4 5 5 5 6 7 8 8 9
I needed to find a way to create this logical index. If all elements of the list of indexes that are not first are false, I will have the necessary logical index.
unlist(lapply(indexes, '[', -1)) [1] 3 7 8 12
You will notice that these positions are all false values ββin the index.
dplyr
I guess it's only fair to add dplyr to the mix:
dat %>% mutate(indx = na.omit(c(T, x != lead(x)))) %>% group_by(ind2=cumsum(indx)) %>% mutate(y=toString(y)) %>% filter(indx)