Question

How to return 5 topmost values from vector in R?

I have a vector and I'm able to return highest and lowest value, but how to return 5 topmost values? Is there a simple one-line solution for this?

 45  112765  45
1 Jan 1970

Solution

 77
> a <- c(1:100)
> tail(sort(a),5)
[1]  96  97  98  99 100
2010-09-11

Solution

 25
x[order(x)[1:5]]
2010-09-11