[R] gsub

Duncan Murdoch murdoch at stats.uwo.ca
Wed Nov 15 14:36:41 CET 2006


On 11/15/2006 8:29 AM, Luis Ridao Cruz wrote:
> R-help,
> 
> I want to remove the following strings
> "cpue" and "nogd"
> 
> string <- c("upsanogd" ,    "toskanogd" ,   "hysunogd"   ,  "konganogd"
>   
>  ,"gullaksnogd" , "longunogd"  ,  "blalongunogd"  , "brosmunogd")
> 
> I could use first : first <- gsub("cpue" , "", string)
> and then : second <- gsub("nogd" , "", first)
> 
> Can it be done at once?

gsub("cpue|nogd", "", string)

See ?regex for a description of the kinds of patterns R can use, in 
particular

"Two regular expressions may be joined by the infix operator |; the 
resulting regular expression matches any string matching either 
subexpression. For example, abba|cde matches either the string abba or 
the string cde. Note that alternation does not work inside character 
classes, where | has its literal meaning."

Duncan Murdoch



More information about the R-help mailing list