maximum values

Asked

Viewed 53 times

3

how do I select the 5 highest values (km in this example) in a data set? I only know the function which() and max(), but they give only the greatest of all, not the biggest 5 for example.

dados <- data.frame(nome = letters[1:11],
                    km = c(10,220,320,310,506,490,120,15,16,67,506))


max(dados$km)
which(dados$km == max(dados$km))

3 answers

5

One option would be:

library(dplyr)

arrange(dados, desc(km))[1:5]

A second option would be:

library(data.table)
setDT(dados)
dados[order(km, decreasing = T)][1:5,]
 

Both generate the same solution:

inserir a descrição da imagem aqui

  • Only detail with the data.table syntax: no commas needed at the end: dados[order(km, decreasing = TRUE)][1:5] works the same.

  • Great! I’ll edit it!

2

A solution using dplyr and maintaining the data frame..

library(dplyr)
     dados %>% 
    # Ordenando os valores
      arrange(-km) %>% 
    # Selecionando os 5 maiores
      slice(1:5)

2

Same principle as solution by Josiane Souza (sort the data and select the first 5 - or last, depending on the sort), but with R base:

tail(sort(dados$km), 5)
# ou
head(sort(dados$km, decreasing = TRUE), 5)
# ou
sort(dados$km, decreasing = TRUE)[1:5]

If you want the highest values without repetition, you can use unique.

tail(unique(sort(dados$km)), 5)
#> [1] 220 310 320 490 506

Browser other questions tagged

You are not signed in. Login or sign up in order to post.