Yes, you can use plyr
as follows:
esoph <- data.frame(agegp = sample(letters[1:2], 20, replace = TRUE),
alcgp = sample(LETTERS[1:2], 20, replace = TRUE),
ncontrols = runif(20))
ddply(esoph, c("agegp", "alcgp"),
function(x){idx <- c(which.min(x$ncontrols),
which.max(x$ncontrols))
x[idx, , drop = FALSE]})
# agegp alcgp ncontrols
# 1 a A 0.03091483
# 2 a A 0.88529790
# 3 a B 0.51265447
# 4 a B 0.86111649
# 5 b A 0.28372232
# 6 b A 0.61698401
# 7 b B 0.05618841
# 8 b B 0.89346943
ddply(esoph, c("agegp", "alcgp"),
function(x){idx <- c(which.min(x$ncontrols),
which.max(x$ncontrols))
x[-idx, , drop = FALSE]})
# agegp alcgp ncontrols
# 1 a A 0.3745029
# 2 a B 0.7621474
# 3 a B 0.6319013
# 4 b A 0.3055078
# 5 b A 0.5146028
# 6 b B 0.3735615
# 7 b B 0.2528612
# 8 b B 0.4415205
# 9 b B 0.6868219
# 10 b B 0.3750102
# 11 b B 0.2279462
# 12 b B 0.1891052
There are possibly many alternatives, e.g. using head
and tail
if your data is already sorted, but this should work.