[R] Q about strsplit and regexp

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.ac.be
Wed Oct 20 15:03:53 CEST 2004


Hi Andy,

may be something like:

x <- "  a b     c "
##########
nx <- nchar(x)
x. <- substring(x, 1:nx, 1:nx)
x.[x.!=" "]

could be helpful.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/396887
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Liaw, Andy" <andy_liaw at merck.com>
To: "R-Help" <r-help at r-project.org>
Sent: Wednesday, October 20, 2004 2:15 PM
Subject: [R] Q about strsplit and regexp


> Dear R-help,
>
> This one is probably a piece of cake for regexp masters.  I'd like 
> to split
> a character vector (for simplicity, say of length one for now) that 
> contains
> fields that are delimited by arbitrary number of white spaces (e.g., 
> "  a b
> c ").  How do I get the character vector that contain the fields? 
> In the
> example I gave, I've tried:
>
>> strsplit("  a b    c ", " +")
> [[1]]
> [1] ""  "a" "b" "c"
>
> I do not want that empty character in the beginning, but couldn't 
> figure out
> how to strip the starting white spaces, other than something ugly 
> like:
>
>> strsplit(sub("^ +", "", "  a b    c "), " +")
> [[1]]
> [1] "a" "b" "c"
>
> Can some kind soul point me to a simpler way?  TIA!!
>
> Best,
> Andy
>
> Andy Liaw, PhD
> Biometrics Research      PO Box 2000, RY33-300
> Merck Research Labs           Rahway, NJ 07065
> andy_liaw <at> merck.com          732-594-0820
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list