Less Value of a Column Set

4

I have a dataset and would like to select only the smallest value between each primary key. Here is the example of my DF:

ORDEM <- c(1,5,2,3,1,10)
GUIA <- c('111','111','333','333','555','555')
COR <- c('AZUL','AMARELO','PRETO','LARANJA','ROSA','VERDE')
DADOS <- data.frame(ORDEM,GUIA,COR)

In this example, there are two records for each key ( GUIA ). Therefore, I would like the result to show only the smallest value from the ORDEM column. The result I hope should be:

ORDEM <- c(1,2,1)
GUIA <- c('111','333','555')
COR <- c('AZUL','PRETO','ROSA')
DADOS_FINAL <- data.frame(ORDEM,GUIA,COR)
    
asked by anonymous 27.11.2018 / 15:27

1 answer

6

With dplyr you can do this:

library(dplyr)
DADOS %>%  group_by(GUIA) %>% filter(ORDEM == min(ORDEM)) %>% ungroup()
# A tibble: 3 x 3
  ORDEM GUIA  COR  
  <dbl> <fct> <fct>
1     1 111   AZUL 
2     2 333   PRETO
3     1 555   ROSA 
    
27.11.2018 / 15:36