How to return 5 topmost values from vector in R?
Asked Answered
I

4

45

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?

Insomuch answered 11/9, 2010 at 20:33 Comment(0)
T
77
> a <- c(1:100)
> tail(sort(a),5)
[1]  96  97  98  99 100
Toolis answered 11/9, 2010 at 21:23 Comment(5)
Or head(sort(a, decreasing=TRUE), 5)Homelike
tail is slightly faster than head and decreasing = TRUE > x <- rnorm(50000000) > system.time(tail(sort(x), 5)) user system elapsed 22.64 0.25 22.95 > system.time(head(sort(x, decreasing = TRUE), 5)) user system elapsed 23.26 0.20 23.51Motorway
@Motorway You should run this more then once and take average time. Cause I think there is no difference (statistically speaking), based on my simulations.Homelike
I get on average 2% faster times for user.self and elapse. The gain on sys.self is 8%. But the relevance on the gain depends on the application.Motorway
Using sort(x, method='quick') is significantly faster, but David's solution below using the partial argument is even faster.Macnamara
O
26
x[order(x)[1:5]]
Orcein answered 11/9, 2010 at 23:7 Comment(1)
should be x[order(x, decreasing = TRUE)[1:5]] but the accepted answer is goodTry
I
5

Yes, head( X, 5) where X is your sorted vector.

Impressionable answered 11/9, 2010 at 20:44 Comment(0)
G
3
tail(sort.int(x, partial=length(x) - 4), 5)

Using sort.int with partial has the advantage of being (potentially) faster by (potentially) not doing a full sort. But in reality, my implementation appears a little slower. Maybe this is because with parameter partial != NULL, shell sort is used rather than quick sort?

> x <- 1:1e6
> system.time(replicate(100, tail(sort.int(x, partial=length(x) - 4), 5)))
   user  system elapsed 
  4.782   0.846   5.668
> system.time(replicate(100, tail(sort(x), 5)))
   user  system elapsed 
  3.643   0.879   4.854 
Gleiwitz answered 12/9, 2010 at 8:7 Comment(1)
If you instead use x<-runif(1e6) you'd see the benefit. Note that those 5 values you get back would indeed be the highest 5, but not necessarily in a sorted order.Macnamara

© 2022 - 2024 — McMap. All rights reserved.