cnauber
cnauber

Reputation: 443

How to return the lowest value and name var from the list in R?

I have a return list:

test<-list(Test A=5421.815, Test B=5032.269, Test n=6602.334 )

Name Type Value
Test double 1 List of lenght 7
Test A double 1 5421.815
Test B double 1 5032.269
Test n double 1 6602.334

The min(unlist(test)) or Reduce(min,test) commands only return the desired value but not the name of the corresponding test.

5032.269

Expected return:

Test B 5032.269

Upvotes: 1

Views: 630

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388907

test[Reduce(min, test) == test]
#$`Test B`
#[1] 5032.269

Or

test[min(unlist(test)) == test]

#$`Test B`
#[1] 5032.269

Upvotes: 1

akrun
akrun

Reputation: 887048

Use which.min to get the index of minimum value and then subset the list with that index

test[which.min(test)]
#$`Test B`
#[1] 5032.269

Upvotes: 1

Related Questions