Using tapply
:
agg <- with(data, tapply(a, nums, FUN = mean))
Then for 1, do:
sort(agg, decreasing = TRUE)
For 2, do:
agg[predetermined.nums]
You can also use aggregate
:
agg <- aggregate(a ~ nums, data, FUN = mean)
Then for 1, do:
agg[order(agg$a, decreasing = TRUE), ]
For 2, do:
agg[match(predetermined.nums, agg$nums), ]