[R] splitting a string into words preserving blanks (using regex)

Eik Vettorazzi E.Vettorazzi at uke.de
Mon Oct 24 16:18:15 CEST 2011


Hi Mark,
here is a way using gsub to insert a "split marker" and strsplit.

strsplit(gsub("([[:alnum:]]+)","|\\1|",c(" some    words to split "))[[1]]

cheers

Am 24.10.2011 15:46, schrieb Mark Heckmann:
> I would like to split a string into words at its blanks but also to preserve all blanks.
> 
> Example:
> 	c(" some    words to split ") 
> should become
> 	c(" ", "some", "   ", "	words", " ", "to" , " ", "split", " ")
> 
> I was not able to achieve this via strsplit() .
> But I am not familiar with regular expressions.
> Is there an easy way to do that using e.g. regex and strsplit?
> 
> Thanks
> Mark
> –––––––––––––––––––––––––––––––––––––––
> Mark Heckmann
> Blog: www.markheckmann.de
> R-Blog: http://ryouready.wordpress.com
> 
> ______________________________________________
> 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.


-- 
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790

--
Pflichtangaben gemäß Gesetz über elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG):

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; Gerichtsstand: Hamburg

Vorstandsmitglieder: Prof. Dr. Guido Sauter (Vertreter des Vorsitzenden), Dr. Alexander Kirstein, Joachim Prölß, Prof. Dr. Dr. Uwe Koch-Gromus 



More information about the R-help mailing list