[R] Manipulate strings reordering some elements

Igor Sosa Mayor joseleopoldo1792 at gmail.com
Tue Mar 13 11:28:12 CET 2012


many thanks, Berend.

It works well... but with a problem because i was not completely clear
in my first email.

It works with cases such as:
Franco (El)
Regueras (Las)

but not with other cases such as:
Fauces de San Andrés (Las)

any hints? or meaybe if it is very complicated, any short explanation of
the perl expression you wrote (as far as I can understand, the point is
this \\3 \\1...).

thx again

On Tue, Mar 13, 2012 at 11:12:24AM +0100, Berend Hasselman wrote:
> 
> On 13-03-2012, at 10:42, Igor Sosa Mayor wrote:
> 
> > Hi R-Users,
> > 
> > I want to manipulate some strings in the following way. I have the
> > following vector with spanish municipalities:
> > 
> > municipios<-c("Allande", "Aller", "Amieva", "Avilés", "Belmonte de
> > Miranda", 
> > "Degaña", "Franco (El)", "Gijón", "Gozón", "Grado", "Grandas de Salime", 
> > "Quirós", "Regueras (Las)", "Ribadedeva", "Ribadesella", "Ribera de
> > Arriba")
> > 
> > The problem is: some names have an article ("Franco (El)", "Regueras
> > (Las)"). Others don't. I want to do the following conversion:
> > 
> > "Regueras (Las)"---> "Las Regueras"
> > 
> > That is: I want to loop through the names, look whether they have a
> > postponed article, extract and delete this article and put it in front
> > of the rest of the name.
> > 
> > Any hints? Thanks in advance.
> 
> 
> gsub("([^\\s]+)\\s*(\\()(.*)(\\))","\\3 \\1", municipios, perl=TRUE)
> 
> 
> Berend
> 

-- 
:: Igor Sosa Mayor   :: joseleopoldo1792 at gmail.com ::
:: GnuPG: 0x69804897 :: http://www.gnupg.org/      ::



More information about the R-help mailing list