R's basic solution is to combine the output of aggregate() with the step merge() . I find the aggregate() formula interface a little more useful than the standard interface, partly because the output names are nicer, so I will use this:
aggregate() step -
maxs <- aggregate(Score ~ Group, data = dat, FUN = max)
and the merge() step is just
merge(maxs, dat)
This gives us the desired result:
R> maxs <- aggregate(Score ~ Group, data = dat, FUN = max) R> merge(maxs, dat) Group Score Info 1 1 3 c 2 2 4 d
You could, of course, insert this into a single line (the intermediate step was more for exposure):
merge(aggregate(Score ~ Group, data = dat, FUN = max), dat)
The main reason I used the formula interface is because it returns a data frame with the correct names for the merge step; these are the column names from the original dat dataset. We need the correct names in the output of aggregate() so that merge() knows which columns in the source and aggregated data frames match.
The standard interface gives odd names, depending on what you call it:
R> aggregate(dat$Score, list(dat$Group), max) Group.1 x 1 1 3 2 2 4 R> with(dat, aggregate(Score, list(Group), max)) Group.1 x 1 1 3 2 2 4
We can use merge() on these outputs, but we need to do more work by telling R which correspond to the columns.
Gavin Simpson Jun 09 '11 at 8:16 2011-06-09 08:16
source share