[R] multiple gsub

Gabor Grothendieck ggrothendieck at gmail.com
Thu Feb 23 14:25:00 CET 2012


On Thu, Feb 23, 2012 at 5:28 AM, TwistedSkies <david.vicary at talbotuw.com> wrote:
> Hi Guys,
>
> I am relatively new to R and was wondering if I could next my gsub command
> in identifying one object
>
> I have data which looks like this:  <name>Taiwan_250km</name>
> I want it to look like this:                 Taiwan_250km
>
> So essentially I just want to gsub '<name>' and  </name> with nothing!
>
> So far I have got this:  PolyNam <-
> unlist(strsplit(gsub("</name>","",PolyRaw[PolyLin],fixed = TRUE)," "))
>
> Which removes the end tag, just wondering how I can nest 2 gsubs to remove
> both?!

Just remove < followed by zero or more of anything except > followed by >

gsub("<[^>]*>", "", "<name>Taiwan_250km</name>")

or using the XML package:

library(XML)
xmlValue(xmlRoot(xmlTreeParse("<name>Taiwan_250km</name>", asText = TRUE)))

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list