Home > Enterprise >  Find column value where maximum value in other column r
Find column value where maximum value in other column r

Time:06-23

How can I find the "Feature" with the corresponding maximum value in the "Similarity" column? I have tried all the tips from other questions but it doesn't work. Thank you!

structure(list(Feature = c("Similarity_CEO", "Similarity_CFO", 
"Similarity_COO", "Similarity_CPO", "Similarity_CTO", "Similarity_CMO_CGO", 
"Similarity_CCO_CSO"), Similarity = c(0.647709116583091, 0.57912317932745, 
0.610563782018354, 0.568011165196869, 0.532707661875697, 0.595690196204727, 
0.575445639485977)), class = "data.frame", row.names = c(NA, 
-7L))

CodePudding user response:

To extract the relevant row:

library(dplyr)
df %>%
  filter(Feature == Feature[which.max(Similarity)])
         Feature Similarity
1 Similarity_CEO  0.6477091

To isolate the relevant Feature value:

df %>%
  filter(Feature == Feature[which.max(Similarity)]) %>%
  pull(Feature)
[1] "Similarity_CEO"

CodePudding user response:

Using slice_max:

library(dplyr)
df %>%
  slice_max(Similarity)

Output:

         Feature Similarity
1 Similarity_CEO  0.6477091
  •  Tags:  
  • r max
  • Related