Skip to content

Extract names from vector according to their values

8 messages · Sverre Stausland, David Winsemius, Jean V Adams +4 more

#
Dear helpers,

I can create a vector with the priority of the packages that came with
R, like this:
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.

Thank you
Sverre
#
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.
#
Hi:

One more possibility:
[1] "Matrix"     "boot"       "class"      "cluster"    "codetools"
 [6] "foreign"    "KernSmooth" "lattice"    "MASS"       "Matrix"
[11] "mgcv"       "nlme"       "nnet"       "rpart"      "spatial"
[16] "survival"
[1] "base"      "compiler"  "datasets"  "graphics"  "grDevices" "grid"
 [7] "methods"   "splines"   "stats"     "stats4"    "tcltk"     "tools"
[13] "utils"

HTH,
Dennis

On Tue, Aug 2, 2011 at 11:21 AM, Sverre Stausland
<johnsen at fas.harvard.edu> wrote:
#
windows 7
R 2.12.1

Is there any easy way to determine if a sting contains nothing but blanks? I need to check a series of strings of various length.

OneBlank <- " "
TwoBlanks <- "  "
ThreeBlanks <- "   "
NoBlanks <- "NoBlanks"

What I want would be a function such as ALLBLANKS that would return the following
ALLBLANKS(OneBlank) would be true (or 1)
ALLBLANKS(TwoBlanks) would be true (or 1)
ALLBLANKS(ThreeBlanks) would be true (or 1)
ALLBLANKS(NoBlanks) would be false (or 0)

Thanks,
John

John David Sorkin M.D., Ph.D.
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)

Confidentiality Statement:
This email message, including any attachments, is for th...{{dropped:6}}
#
On Aug 2, 2011, at 8:07 PM, John Sorkin wrote:

            
> bvec <- c(OneBlank, TwoBlanks ,ThreeBlanks ,NoBlanks)
 > grepl(" +", bvec)
[1]  TRUE  TRUE  TRUE FALSE
#
On Tue, Aug 2, 2011 at 5:07 PM, John Sorkin <JSorkin at grecc.umaryland.edu> wrote:
ALLBLANKS = function(x) {x==paste(rep(" ", nchar(x)), collapse = "")}
[1] TRUE
[1] TRUE
[1] FALSE

HTH,

Peter
#
If the question is that the string contains all blanks, then a regular
expression will probably be best:
[1]  TRUE  TRUE  TRUE FALSE FALSE

        
On Tue, Aug 2, 2011 at 8:07 PM, John Sorkin <JSorkin at grecc.umaryland.edu> wrote: