Sverre Stausland
2011-Aug-02 18:21 UTC
[R] Extract names from vector according to their values
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.vectorbase 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
David Winsemius
2011-Aug-02 18:34 UTC
[R] Extract names from vector according to their values
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
Jean V Adams
2011-Aug-02 18:34 UTC
[R] Extract names from vector according to their values
Sverre, Try this: my.list <- split(names(my.vector), my.vector) my.list$base my.list$recommended Jean `·.,, ><(((º> `·.,, ><(((º> `·.,, ><(((º> Jean V. Adams Statistician U.S. Geological Survey Great Lakes Science Center 223 East Steinfest Road Antigo, WI 54409 USA From: Sverre Stausland <johnsen@fas.harvard.edu> To: r-help@r-project.org Date: 08/02/2011 01:24 PM Subject: [R] Extract names from vector according to their values Sent by: r-help-bounces@r-project.org 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.vectorbase 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 ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]
Dennis Murphy
2011-Aug-02 18:41 UTC
[R] Extract names from vector according to their values
Hi: One more possibility:> names(my.vector[grep('recommended', my.vector)])[1] "Matrix" "boot" "class" "cluster" "codetools" [6] "foreign" "KernSmooth" "lattice" "MASS" "Matrix" [11] "mgcv" "nlme" "nnet" "rpart" "spatial" [16] "survival"> names(my.vector[grep('base', my.vector)])[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:> 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. > > Thank you > Sverre > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
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:> 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)> bvec <- c(OneBlank, TwoBlanks ,ThreeBlanks ,NoBlanks) > grepl(" +", bvec) [1] TRUE TRUE TRUE FALSE -- David Winsemius, MD West Hartford, CT
On Tue, Aug 2, 2011 at 5:07 PM, John Sorkin <JSorkin at grecc.umaryland.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)ALLBLANKS = function(x) {x==paste(rep(" ", nchar(x)), collapse = "")}> ALLBLANKS(" ")[1] TRUE> ALLBLANKS(" ")[1] TRUE> ALLBLANKS(" A ")[1] FALSE HTH, Peter
If the question is that the string contains all blanks, then a regular expression will probably be best:> OneBlank <- " " > TwoBlanks <- " " > ThreeBlanks <- " " > NoBlanks <- "NoBlanks" > bvec <- c(OneBlank, TwoBlanks ,ThreeBlanks ,NoBlanks) > bvec <- c(OneBlank, TwoBlanks ,ThreeBlanks ,NoBlanks, " A ") > grepl("^ *$", bvec)[1] TRUE TRUE TRUE FALSE FALSE>On Tue, Aug 2, 2011 at 8:07 PM, John Sorkin <JSorkin at grecc.umaryland.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 ...{{dropped:18}}