Find value corresponding to maximum in other column [duplicate]

Using dplyr:

# install.packages(dplyr)
library(dplyr)

df %>% 
    filter(x == max(y)) %>% # filter the data.frame to keep row where x is maximum
    select(x) # select column y

Alternatively to return a vector

df %>% 
    filter(x == max(y)) %>% 
    pull(x) # pull the variable y

using base R:

df[df$x == max(df$y), "x"]

Leave a Comment