[R] gsub pattern?

Barry Rowlingson B.Rowlingson at lancaster.ac.uk
Fri Jan 21 11:03:28 CET 2005


Christian Schulz wrote:
>  Where is my mistake?
> 
> bcode <- gsub("/^AUTO.*/","1",MyStringVector,ignore.case=T,extended=T)
> 

  You dont need the slashes! You've been looking at documentation for 
Perl regular expression replacements, I guess.

  help(gsub) may have showed you the way. Here's how to do it:

  > MyStringVector=c("AUTOBAHN","NAUTON","FOO","AUTOGRAPH")

# wrong way:

  > gsub("/^AUTO.*/","1",MyStringVector,ignore.case=T,extended=T)
  [1] "AUTOBAHN"  "NAUTON"    "FOO"       "AUTOGRAPH"

# dont slash all over the regexp:

  > gsub("^AUTO.*","1",MyStringVector,ignore.case=T,extended=T)
  [1] "1"      "NAUTON" "FOO"    "1"

  Is that what you're after?

Baz




More information about the R-help mailing list