Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name of the Minimum value in a named vector

Tags:

r

I have a named vector:

v <- c("morning"=80, "noon"=20, "night"=40)

printing min(v) gives

[1] 20

I want to get this instead:

noon
    20

Is there a simple way?

like image 875
devlent Avatar asked Sep 04 '15 17:09

devlent


1 Answers

v[which.min(v)] will give you the output you listed. But if you just want the name, and not the value, then do names(v)[which.min(v)].

like image 88
eipi10 Avatar answered Sep 18 '22 20:09

eipi10