Sure. Something like this should do the trick:

# CREATE A REPRODUCIBLE EXAMPLE! df <- data.frame(year = c("2001", "2003", "2001", "2003", "2003"), pixel = c("a", "b", "a", "b", "a"), cumsum = c(99, 99, 98, 99, 99), numbercomps=1:5) df # year pixel cumsum numbercomps # 1 2001 a 99 1 # 2 2003 b 99 2 # 3 2001 a 98 3 # 4 2003 b 99 4 # 5 2003 a 99 5 # EXTRACT THE SUBSET YOU'D LIKE. res <- subset(df, cumsum>=99) res <- subset(res, subset = !duplicated(res[c("year", "pixel")]), select = c("pixel", "year", "numbercomps")) # pixel year numbercomps # 1 a 2001 1 # 2 b 2003 2 # 5 a 2003 5

**EDIT** Also, for those interested in `data.table`

, there is this:

library(data.table) dt <- data.table(df, key="pixel, year") dt[cumsum>=99, .SD[1], by=key(dt)]