Re: [R] R newbie: how to replace string/regular expression

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Sun, 02 Nov 2008 07:55:00 -0500

Your gsub example is almost exactly what gsubfn in the gsubfn package does. gsubfn like gsub except the replacement string is a function:

> library(gsubfn)
> gsubfn("(.*)B$", ~ as.numeric(x) * 10e6, d, ignore.case = TRUE)

[1] "120.0M" "11.01m" "2.097e+09" "100.00k" "50"

Also there are examples very similare to this

  1. at the end of section 2 of vignette("gsubfn")
  2. in demo("gsubfn-si")

Also see the gsubfn home page:
http://gsubfn.googlecode.com

Also note that if you want to return the values rather than transform and reinsert them then strapply in the same package can do that.

On Sun, Nov 2, 2008 at 3:43 AM, Krishna Dagli/Krushna Dagli <krishna.dagli_at_gmail.com> wrote:
> Hello;
>
> I am a R newbie and would like to know correct and efficient method for
> doing string replacement.
>
> I have a large data set, where I want to replace character "M", "b",
> and "K" (currency in Million, Billion and K) to millions. That is
> 209.7B with (209.7 * 10e6) and 100.00K with (100.00 *1/100)
> and etc..
>
> d <- c("120.0M", "11.01m", "209.7B", "100.00k", "50")
>
> This works that is it removes "b/B",
>
> gsub ("(.*)(B$)", "\\1", d, ignore.case=T, perl=T)
>
> but
>
> gsub ("(.*)(B$)", as.numeric("\\1") * 10e6, d, ignore.case=T, perl=T)
>
> does not work. I tried with sprintf and other combination of as.numeric but
> that fails, how to use \\1 and multiply with 10e6??
>
> The other solution is :
>
> location <- grep ("M", d, ignore.case=T)
> y <- sub("M", "", d, ignore.case=T)
> y[location]<-y[location] * 10e6
>
> Is the second solution faster or (if) combination of grep along with
> multiply (if it works) is faster? Or what is the most efficient method
> to do something like this in R?
>
> Thanks and Regards
> Krishna
>
> ______________________________________________
> 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.
>



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. Received on Sun 02 Nov 2008 - 13:03:54 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 02 Nov 2008 - 15:30:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive