How to extract the best values

Hello !

I would like to know how to extract the three highest values from a list of numbers, in R, in a simple way without downloading packs.

Thank you !

As far as I know there is no function in base R for sorting a list object but you can do this easily with vectors.

x <- c(1, 2, 4, 5, 6, 7, 8, 9)
sort(x, decreasing = TRUE)[1:3]
#> [1] 9 8 7

Created on 2019-05-13 by the reprex package (v0.2.1)

Hi andresrcs !

Thank you very mutch !


Thank you for your previous help, I would like to complete my script but a blockage persists

I would like to write : the top three score are: the best (here QNKLLLVS), the best -1 (here TYLFRLMA) and the best - 2 (here PNSGPFSF), with value: 34, 30 and 28. But I can not unblock the error in my script.

TATLQAKA at like score 22
EARKIFRF at like score 21
SKKELKEL at like score 24
PNSGPFSF at like score 28
TYLFRLMA at like score 30
QNKLLLVS at like score 34
QGLRAGSF at like score 27

segments_data <- read.table("segments.txt", header = TRUE)
cat("the top three score are:", segments_data[i[1],], ",", segments_data[i[2],], "and", segments_data[i[3],], "with respective scores:", sort(scores, decreasing = TRUE) [1],",", sort(scores, decreasing = TRUE) [2], "and", sort(scores, decreasing = TRUE) [3],".", "\n")

Do you have an idea?

Thank you

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.