[R] grep, gsub and metacharacters

Greg Snow Greg.Snow at imail.org
Fri Aug 22 19:22:54 CEST 2008


Try this:

> myvector<-c("ajkhfkiuwe","Variable(kg/min)")
> gsub( "\\(kg/min\\)", "va", myvector )

Does that come close to what you want?  If not, maybe an example of what you want the result to look like,

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
(801) 408-8111



> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Judith Flores
> Sent: Friday, August 22, 2008 10:57 AM
> To: RHelp
> Subject: [R] grep, gsub and metacharacters
>
> Hello,
>
>    I have an expression that I want to substitute for something else.
>
> myvector<-c("ajkhfkiuwe","Variable(kg/min)")
>
> if I use the following code to extract "variable(kg/min)" and
> substitute it for "va"
>
> gsub(myvector[grep("var", myvector, ignore=T)], "va", myvector)
>
>  grep identifies the element of the vector that matches my
> query, but it won't substitute the value. I have been reading
> the help pages for regular expression, but still can't figure
> out the syntax to read parenthesis and forward slashes, which
> are considered metacharacters.
>
>
> As usual, thank you for your help,
>
> Judith
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list