Home:ALL Converter>Finding key:value pair for minimum value in R list

Finding key:value pair for minimum value in R list

Ask Time:2020-08-18T08:48:13         Author:iskandarblue

Json Formatter

I have a list - distances in R. What is the simplest way to return the key-value pair in the list that contains the minimum value?

I understand the actual minimum value can be found with min(unlist(distances)), but is there a one line solution to finding the corresponding key and minimum value in the list? The output should be something like [[2]] , 36.40016

distances
[[1]]
         [,1]
[1,] 144.7077

[[2]]
         [,1]
[1,] 36.40016


> dput(distances)
list(structure(144.707681719953, .Dim = c(1L, 1L)), structure(36.4001632486179, .Dim = c(1L, 
1L)))

Author:iskandarblue,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/63460386/finding-keyvalue-pair-for-minimum-value-in-r-list
Ronak Shah :

You can use which.min to get the index of minimum value after unlisting :\nwhich.min(unlist(distances))\n#[1] 2\n",
2020-08-18T00:50:35
yy