Re: [R] strsplit, keeping delimiters

From: Martin Morgan <mtmorgan_at_fhcrc.org>
Date: Sat, 14 Jun 2008 08:20:00 -0700

"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

______________________________________________
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.
Received on Sat 14 Jun 2008 - 15:55:07 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 14 Jun 2008 - 17:30:41 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive