[R] Splitting a character vector.

John Kane jrkrideau at inbox.com
Sun Jul 8 00:19:44 CEST 2012


Thanks Jeff.
I actually had that figured out after a good hour of pounding my head against the wall but I still could not seem to get the syntax correct.  I think I misunderstand strpsplt() just enough to keep making dumb mistakes.

John Kane
Kingston ON Canada


> -----Original Message-----
> From: jdnewmil at dcn.davis.ca.us
> Sent: Sat, 07 Jul 2012 15:12:16 -0700
> To: ruipbarradas at sapo.pt, jrkrideau at inbox.com
> Subject: Re: [R] Splitting a character vector.
> 
> Just to clarify, the regex engine wants to see a \ before the ( if it is
> to treat it as an ordinary character. However, the source code
> interpreter also treats \ as an escape character. In order to get a \
> into the string, you have to escape it. So it takes two \ characters in
> source code to obtain one \ character in memory where the regex code can
> "see" it.
> ---------------------------------------------------------------------------
> Jeff Newmiller                        The     .....       .....  Go
> Live...
> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live
> Go...
>                                       Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.
> rocks...1k
> ---------------------------------------------------------------------------
> Sent from my phone. Please excuse my brevity.
> 
> Rui Barradas <ruipbarradas at sapo.pt> wrote:
> 
> >Hello,
>> 
> >Try the following.
>> 
> >open.par <- " \\("  # with a blank before '('
> >close.par <- "\\)"
> >result <- strsplit(sub(close.par, "", dd1), open.par)
>> 
>> 
> >Why the two '\\'? Because '(' is a meta-character so it must be
> >escaped.
> >But '\' is a meta character so it must also be escaped.
>> 
> >Then choose the right way to separate the two, maybe something like
>> 
> >ix <- rep(c(TRUE, FALSE), length(result))
> >unlist(result)[ix]
> >unlist(result)[!ix]
>> 
>> 
> >Hope this helps,
>> 
> >Rui Barradas
>> 
> >Em 07-07-2012 22:37, John Kane escreveu:
>>> I am lousy at simple regex and I have not found a solution to a
> >simple problem.
>>> 
>>> I have a vector with some character values that I want to split.
>>> Sample data
>>> dd1  <-  c( "XXY (mat harry)","XXY (jim bob)", "CAMP (joe blow)",
> >"ALP (max jack)")
>>> 
>>> Desired result
>>> dd2  <-  data.frame( xx = c("XXY", "XXY", "CAMP", "ALP"), yy = c("mat
> >harry", "jim bob" , "joe blow", "max jack"))
>>> 
>>> I thought I should be able to split the characters with strsplit but
> >either I am misunderstanding the function or don't know how to escape a
> >"(" properly in an effort to at least get   "XXY" "(mat harry)"
>>> 
>>> Any pointers would be appreciated
>>> Thanks
>>> John Kane
>>> Kingston ON Canada
>>> 
>>> ____________________________________________________________
>>> FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas
> >on your desktop!
>>> 
>>> ______________________________________________
>>> 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.
>>> 
>> 
> >______________________________________________
> >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.

____________________________________________________________
FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!



More information about the R-help mailing list