I have the following exemplary data frame:
df <- as.data.frame(cbind(Month <- c("Jun", "Jul", "Aug", "Oct", "Jun", "Aug", "Feb"),ID <- c(1,1,1,2,2,2,2), No_ind <- c(8,5,2,15,10,9,2)))
names(df) <- c('month', 'ID', 'No_ind')
I have already sorted the No_ind column in an descending order. What I now want to achieve is the following. For every group (ID), I want to get all rows within a group until the cumulative sum of the number of individuals (No_ind) has reached 15 individuals (including the row where we hit 15 ind). I don't want the sum to go beyond group borders, but start anew for each group.
I then want to be able to extract the rows (month * plot combinations) that are needed for getting to 15 individuals. If the algorithm is correct, it chooses all rows (month * plot) combinations of group 1, only the first month*plot combination of group 2.
Many thanks in advance. I really appreciate any help!