Home > Software engineering >  select min value in two groups
select min value in two groups

Time:10-08

I want to find the lowest value for each id, for each rank.

I´ve tried:

DF%>%group_by(id)%>%filter(id=min(id))

But can´t figure out where to put the rank condition.

   DF<-data.frame(id=c(1,1,1,1,2),rank=c("1","1","2","3","1"),code=c("0","1","1","0","0"))
    
  id rank code
1  1    1    0
2  1    1    1
3  1    2    1
4  1    3    0
5  2    1    0

Desired output:

  id rank code 
1  1    1    0   
2  1    2    1   
3  1    3    0   
4  2    1    0
  

Best wishes /H

CodePudding user response:

We need to group by both 'id' and 'rank' and use slice_min to get the min value of 'code'

library(dplyr)
DF %>% 
   group_by(id, rank)  %>% 
   slice_min(n = 1, order_by = code) %>%
   ungroup

-output

# A tibble: 4 × 3
     id rank  code 
  <dbl> <chr> <chr>
1     1 1     0    
2     1 2     1    
3     1 3     0    
4     2 1     0   

Or with filter

DF %>%
    group_by(id, rank) %>%
    filter(code == min(code)) %>%
     ungroup

Or may use distinct after arrangeing

DF %>% 
   arrange(id, rank, code) %>%
   distinct(id, rank, .keep_all = TRUE)
  id rank code
1  1    1    0
2  1    2    1
3  1    3    0
4  2    1    0
  •  Tags:  
  • r
  • Related