How do I compute order statistics by group in R. I want to aggregate results according to a column and then return only 1 row per group. That row should be the n-th element of the group according to some ordering. Ideally I would like to use base functions only.
x <- data.frame(Group=c("A","A", "A", "C", "C"),
Name=c("v", "u", "w", "x", "y"),
Quantity=c(3,3,4,2,0))
> x
Group Name Quantity
1 A v 3
2 A u 3
3 A w 4
4 C x 2
5 C y 0
I want to take nth highest based on an ordering on Quantity and then Name. For N=2 this is
Group Name Quantity
1 A u 3
5 C y 0
For N=1
Group Name Quantity
3 A w 4
4 C x 2
I tried the following but I get a uninformative error message.
aggregate.data.frame(x, list(x$Group), function(y){ max(y[,'Quantity'])})
Error in `[.default`(y, , "Quantity") (from #1) : incorrect number of dimensions"
I went with
per someone else’s suggestion. I found it suited my thought process a bit better than the other posted solutions (which I appreciate).