[R] strsplit with a vector split argument

Hervé Pagès hpages at fhcrc.org
Wed Sep 18 20:27:00 CEST 2013


On 09/18/2013 09:04 AM, arun wrote:
> Hi,
> You could try:
>
> strsplit(c("a,b;c","d;e,f"),",|;")

He did it with "[,;]" which is equivalent but slightly faster. The more
characters you have inside the square brackets, the more beneficial it
is to use [abcde] over a|b|c|d|e. It's also more compact and easier to
read.

H.

> #[[1]]
> #[1] "a" "b" "c"
> #
> #[[2]]
> #[1] "d" "e" "f"
> A.K.
>
>
>
> ----- Original Message -----
> From: Sam Steingold <sds at gnu.org>
> To: r-help at r-project.org
> Cc:
> Sent: Wednesday, September 18, 2013 11:42 AM
> Subject: [R] strsplit with a vector split argument
>
> Hi,
> I find this behavior unexpected:
> --8<---------------cut here---------------start------------->8---
>> strsplit(c("a,b;c","d;e,f"),c(",",";"))
> [[1]]
> [1] "a"   "b;c"
>
> [[2]]
> [1] "d"   "e,f"
> --8<---------------cut here---------------end--------------->8---
> I thought that it should be identical to this:
> --8<---------------cut here---------------start------------->8---
>> strsplit(c("a,b;c","d;e,f"),"[,;]")
> [[1]]
> [1] "a" "b" "c"
>
> [[2]]
> [1] "d" "e" "f"
> --8<---------------cut here---------------end--------------->8---
> Is this a bug or did I misunderstand the docs?
> Thanks!
>

-- 
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpages at fhcrc.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319



More information about the R-help mailing list