[R] Is a string all blanks?

jim holtman jholtman at gmail.com
Wed Aug 3 02:30:37 CEST 2011


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}}



More information about the R-help mailing list