[R] Extracting numbers from somewhere within strings

Lutz Prechelt prechelt at pcpool.mi.fu-berlin.de
Wed Apr 28 11:58:06 CEST 2004


Hello everybody,

I have a bunch of strings like this:
"IBM POWER4+ 1.9GHz"                  
"IBM RS64-III 500MHz"              
"IBM RS64-IV 600 MHz"                 
"IBM RS64 IV 750MHz"               
"Intel Itanium 2 Processor 6M 1.5GHz" 
"Intel Itanium2 1 Ghz"             
"Intel Itanium2 1.5GHz"               
"Intel MP 1.6GHz"                   

I want to extract the processor speed.

I am using
  grep("MHz", tpc$cpu, ignore.case=T)
  grep("GHz", tpc$cpu, ignore.case=T)
to extract the unit, because there are only these two.

But how to extract the number before it?
(I am using R 1.8.0)

In Perl one would match a regexp such as
  /([0-9.]+) ?[MG][Hh][Zz]/
and then obtain the number as $1.
But the capability of returning $1 is apparently not
implemented in grep() or any other function I could find.

How is it best done?

Thanks in advance,

  Lutz


Prof. Dr. Lutz Prechelt;  prechelt at inf.fu-berlin.de
Institut fuer Informatik; Freie Universitaet Berlin
Takustr. 9; 14195 Berlin; Germany
+49 30 838 75115; http://www.inf.fu-berlin.de/inst/ag-se/




More information about the R-help mailing list