[R] strsplit, keeping delimiters

Martin Morgan mtmorgan at fhcrc.org
Sat Jun 14 17:20:00 CEST 2008


"hadley wickham" <h.wickham at gmail.com> writes:
n
> On Sat, Jun 14, 2008 at 12:55 AM, Gabor Grothendieck
> <ggrothendieck at gmail.com> wrote:
>> Try this:
>>
>>> library(gsubfn)
>>> x <- "A: 123 B: 456 C: 678"
>>> strapply(x, "[^ :]+[ :]|[^ :]+$")
>> [[1]]
>> [1] "A:"   "123 " "B:"   "456 " "C:"   "678"

Also

> strsplit(x, "(?<=[0-9:] )", perl=TRUE)
[[1]]
[1] "A: "  "123 " "B: "  "456 " "C: "  "678" 

which uses perl's zero-length lookbehind to match "" preceed by a
digit or : and then a space. This is not quite what you asked for

> I'd like to get

> c("A:", "123 ", "B: ", "456 ", "C: ", 678)

(no space after A:) or what Gabor offered (no spaces after :) but maybe
what you intended?

Martin

>>
>> and check out the gsubfn home page at:
>>
>> http://gsubfn.googlecode.com
>
> Thanks Gabor!
> Hadley
>
> -- 
> http://had.co.nz/
>
> ______________________________________________
> 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.

-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M2 B169
Phone: (206) 667-2793



More information about the R-help mailing list