[R] Q about strsplit and regexp

Jean-Pierre Muller Jean-Pierre.Mueller at unil.ch
Wed Oct 20 14:49:00 CEST 2004


Hello,

in the function ttda.segmentation of ttda 
<http://wwwpeople.unil.ch/jean-pierre.mueller/>

i use:

     #compute occurences
     occurences <- unlist(strsplit(textlines[1:length(textlines)],
         grep.sep, TRUE))
     #delete empty lines
     occurences <- occurences[nchar(occurences) > 0]

HTH.


Le 20 oct. 04, à 14:15, Liaw, Andy a écrit :

> 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
>
>
-- 
Jean-Pierre Müller
SSP / BFSH2 / UNIL / CH - 1015 Lausanne
Voice:+41 21 692 3116 / Fax:+41 21 692 3115

Please avoid sending me Word or PowerPoint attachments.
  See http://www.fsf.org/philosophy/no-word-attachments.html
S'il vous plaît, évitez de m'envoyer des attachements au format Word ou 
PowerPoint.
  Voir http://www.fsf.org/philosophy/no-word-attachments.fr.html




More information about the R-help mailing list