0

I have the following data:

library(tidyverse)

df1 <- tibble(
    title = c("AA", "AA", "AA", "B", "C", "D", "D"),
    rate = c(100, 100, 100, 95, 92, 90, 90),
    name = c("G", "N", "E", "T", "O", "W", "L"),
    pos = c(10, 1, 2, 2, 3, 5, 4)

)

  title  rate name    pos
  <chr> <dbl> <chr> <dbl>
  AA      100 G        10
  AA      100 N         1
  AA      100 E         2
  B        95 T         2
  C        92 O         3
  D        90 W         5
  D        90 L         4

I want to find out at every title which name has the biggest pos value.

So, for title AA, it would be G, for title B, it would be T, for title C it would be O and for title D it would be W.

George
  • 5,620
  • 13
  • 72
  • 137
  • It's a dup but not with the linked by @kath, try `df1 %>% group_by(title) %>% summarise(mn=name[which.max(pos)])` – A. Suliman Oct 25 '19 at 10:37

1 Answers1

1

For B it should be "T"?

df1 %>% group_by(title) %>% top_n(1,pos) %>% pull(name)
StupidWolf
  • 34,518
  • 14
  • 22
  • 47