[R] Splitting a character vector.

John Kane jrkrideau at inbox.com
Sun Jul 8 00:24:56 CEST 2012


How totaly obvious once you tell me!  I would have spend days trying to figure it out.

I think I have a total mental block on regex and their derivatives.

Thanks very much.

John Kane
Kingston ON Canada


> -----Original Message-----
> From: ruipbarradas at sapo.pt
> Sent: Sat, 07 Jul 2012 23:21:19 +0100
> To: jrkrideau at inbox.com
> Subject: Re: [R] Splitting a character vector.
> 
> It's an empty character string, meant to substitute nothing for
> close.par, to get rid of it.
> 
> Rui Barradas
> 
> Em 07-07-2012 23:17, John Kane escreveu:
>> No sorry Rui,
>> 
>> In the expression result <- strsplit(sub(close.par, "", dd1), open.par)
>> there is  close.par, ''", open.par
>> 
>> I probably am just blind but I don't understand what it is doing.
>> 
>> 
>> 
>> John Kane
>> Kingston ON Canada
>> 
>> 
>>> -----Original Message-----
>>> From: ruipbarradas at sapo.pt
>>> Sent: Sat, 07 Jul 2012 23:08:19 +0100
>>> To: jrkrideau at inbox.com
>>> Subject: Re: [R] Splitting a character vector.
>>> 
>>> Hello,
>>> 
>>> Sorry, but I don't understand, you're asking about 4 single quotes, the
>>> double quotes in open.par are just opening and closing the pattern, a
>>> character string.
>>> 
>>> Rui Barradas
>>> 
>>> Em 07-07-2012 23:03, John Kane escreveu:
>>>> Thanks Rui
>>>> It works perfectly so far on the test and real data.
>>>> 
>>>> The annoying thing is that I had tried , or thought I'd tried the
>>>> open.par format and keep getting an error.
>>>> 
>>>>    It looks like I had failed to add the '''',  in the term. What is
>>>> it
>>>> doing?
>>>> 
>>>> 
>>>> 
>>>> John Kane
>>>> Kingston ON Canada
>>>> 
>>>> 
>>>>> -----Original Message-----
>>>>> From: ruipbarradas at sapo.pt
>>>>> Sent: Sat, 07 Jul 2012 22:55:41 +0100
>>>>> To: jrkrideau at inbox.com
>>>>> Subject: Re: [R] Splitting a character vector.
>>>>> 
>>>>> 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.
>>>>>> 
>>>> 
>>>> ____________________________________________________________
>>>> FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas
>>>> on
>>>> your desktop!
>>>> Check it out at http://www.inbox.com/marineaquarium
>>>> 
>>>> 
>> 
>> ____________________________________________________________
>> FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on
>> your desktop!
>> Check it out at http://www.inbox.com/marineaquarium
>> 
>>

____________________________________________________________
GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys
Works with AIM®, MSN® Messenger, Yahoo!® Messenger, ICQ®, Google Talk™ and most webmails



More information about the R-help mailing list