Skip to content
Prev 267407 / 398502 Next

Extract names from vector according to their values

On Aug 2, 2011, at 2:21 PM, Sverre Stausland wrote:

            
> names( my.vector[which(my.vector=="recommended")])
  [1] "boot"       "class"      "cluster"
  [4] "codetools"  "foreign"    "KernSmooth"
  [7] "lattice"    "MASS"       "Matrix"
[10] "mgcv"       "nlme"       "nnet"
[13] "rpart"      "spatial"    "survival"

Note that some people may tell you that this form below should be  
preferred because the 'which' is superfluous. It is not. The "["  
function returns all the NA's fr reasons that are unclear to me. It is  
wiser to use `which` so that you get numerical indexing.
 > names(my.vector[my.vector=="recommended"])

On my system it produces 493 items most of them NA's.