[R] Splitting a character vector.

John Kane jrkrideau at inbox.com
Sun Jul 8 15:57:45 CEST 2012


Works perfectly. Thank you very much indeed.

John Kane
Kingston ON Canada


> -----Original Message-----
> From: dwinsemius at comcast.net
> Sent: Sat, 7 Jul 2012 21:45:58 -0400
> To: jrkrideau at inbox.com
> Subject: Re: [R] Splitting a character vector.
> 
> 
> On Jul 7, 2012, at 5:37 PM, John Kane wrote:
> 
>> 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"))
> 
> data.frame(xx=sub("(\\s\\(.+$)", "", dd1),
>             yy=sub("(.+)(\\s\\()(.+)(\\)$)", "\\3", dd1) )
>      xx        yy
> 1  XXY mat harry
> 2  XXY   jim bob
> 3 CAMP  joe blow
> 4  ALP  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)"
>> 
> 
> 
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT

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



More information about the R-help mailing list