[R] find numbers in a line with letters

Steve Lianoglou mailinglist.honeypot at gmail.com
Thu Aug 27 00:48:36 CEST 2009


Hi,

On Aug 26, 2009, at 6:38 PM, Martin Batholdy wrote:

> hi,
>
> is there an easy way to extract numbers from a string?
>
> for example I have;
> "this Item costs 3.32 Dollars"
>
> is there an easy way to extract the 3.32 as a number?

Regular expressions to the rescue?

Perhaps you'll need to fine tune it, but see here:

R> gregexpr("(\\d+(\\.\\d+)?)", "this Item costs 3.32 Dollars", perl=T)
[[1]]
[1] 17
attr(,"match.length")
[1] 4

R> gregexpr("(\\d+(\\.\\d+)?)", "this Item costs 3.32 Dollars, that  
item costs 10.12 dollars", perl=T)
[[1]]
[1] 17 47
attr(,"match.length")
[1] 4 5

R> gregexpr("(\\d+(\\.\\d+)?)", "this Item costs 3.32 Dollars, that  
item costs 10 dollars even, ", perl=T)
[[1]]
[1] 17 47
attr(,"match.length")
[1] 4 2

R> gregexpr("(\\d+(\\.\\d+)?)", "this one is free ", perl=T)
[[1]]
[1] -1
attr(,"match.length")
[1] -1

HTH,
-steve

--
Steve Lianoglou
Graduate Student: Computational Systems Biology
   |  Memorial Sloan-Kettering Cancer Center
   |  Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact




More information about the R-help mailing list