Message-ID: <797129A3-9633-4943-AB53-98DC62A808FA@comcast.net>
Date: 2011-08-02T18:34:19Z
From: David Winsemius
Subject: Extract names from vector according to their values
In-Reply-To: <CACtaF7yVxf1O5SaGGGhXoVDXW5dW0gZNzV+LEsJ991szynNqjQ@mail.gmail.com>
On Aug 2, 2011, at 2:21 PM, Sverre Stausland wrote:
> Dear helpers,
>
> I can create a vector with the priority of the packages that came with
> R, like this:
>
>> installed.packages()[,"Priority"]->my.vector
>> my.vector
> base boot class cluster codetools
> "base" "recommended" "recommended" "recommended" "recommended"
> compiler datasets foreign graphics grDevices
> "base" "base" "recommended" "base" "base"
> grid KernSmooth lattice MASS Matrix
> "base" "recommended" "recommended" "recommended" "recommended"
> methods mgcv nlme nnet rpart
> "base" "recommended" "recommended" "recommended" "recommended"
> spatial splines stats stats4 survival
> "recommended" "base" "base" "base" "recommended"
> tcltk tools utils
> "base" "base" "base"
>
> How can I extract the names from this vector according to their
> priority? I.e. I want to create a vector from this with the names of
> the "base" packages, and another vector with the names of the
> "recommended" packages.
> 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.
--
David Winsemius, MD
West Hartford, CT