Alexander
Alexander

Reputation: 4645

Returning the group with matching condition

hi I'm trying to filter a condition and want to extract it's group.

Something like

V <- rnorm(30)
gr = rep(seq(1,3),each=10)
df <- data.frame(V,gr)

min_num <- df%>%
  group_by(gr)%>%
  filter(rank(V,ties.method="min")==1)

returning this

     V    gr
      (dbl) (int)
1 -1.134910     1
2 -1.598005     2
3 -1.317898     3

when I do

  filter(V==min(V))

also returning the same result. How can I get the group that shows min V value. Group number is important. In this case the code should return only 2nd group.

Upvotes: 0

Views: 50

Answers (1)

Marta
Marta

Reputation: 3162

You have to ungruoup it earlier (if not it returns minimum in each group):

min_num %>% 
    ungroup() %>% 
    filter(V==min(V))

Upvotes: 1

Related Questions