drake
drake

Reputation: 259

Finding Largest Element of a Matrix in R

I am using R for the first time and need to write a function that takes in a matrix with 2 columns. For example

col1 col2
  2   0.2
  3   0.2
  4   0.3
  5   0.1
  6   0.2

Based on which number is highest in col2 (which is 0.3), I need to return the corresponding number in col1 (which is 4). How can I go about doing this in R?

Upvotes: 1

Views: 110

Answers (1)

akrun
akrun

Reputation: 887118

We could also order the dataset by the second column and get the first element

df1[order(-df1[,'col2']),1][1]
#[1] 4

Upvotes: 2

Related Questions