Solved it another way, without apply:
data2 <- data[order(data$state.region,-data$Population),] cx <- as.numeric(data2$state.region) data2$rank <- cumsum(rep(1,length(cx)))-match(cx,cx) + 1 all.equal(data2==data) Bart -- View this message in context: http://r.789695.n4.nabble.com/Create-counter-variable-for-subsets-without-a-loop-tp2220663p2221052.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.