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

Krishna Dagli/Krushna Dagli krishna.dagli at gmail.com
Sun Nov 2 09:43:14 CET 2008


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



More information about the R-help mailing list