How to select x value of max y in R

Viewed 352

I have a table with an x and y value as follows:;

cbm <- captialbikedata %>%
    group_by(hr) %>%
    summarize(users = sum(registered))
x y
1 23
2 45

in this case how would I select 2? (I want to select the x value with the highest y value and then put it in a red file)

1 Answers

We can use slice_max

library(dplyr)
red <- cbm %>%
    ungroup %>%
    slice_max(y) %>%
    pull(x)
Related