Skip to content

Generalizing a regex for retrieving numbers with and without scientific notation

2 messages · Morway, Eric, Marc Schwartz

#
On Feb 19, 2014, at 12:26 PM, Morway, Eric <emorway at usgs.gov> wrote:

            
If the individual elements of the vector are either numeric or non-numeric, why not just use:
[1]           NA           NA 6.301343e+00 6.366305e+00 6.431268e+00
[6] 6.496230e+00 6.561192e+00 6.626155e+00 9.091117e-06
Warning message:
NAs introduced by coercion 


The non-numeric elements are returned as NA's, which you can remove by using ?na.omit.

The only reason to use a regex would be if the individual elements themselves contained both numeric and non-numeric characters. If you then want to explicitly format numeric output (which would yield a character vector), you can use ?sprintf or ?format. Keep in mind the difference between how R *PRINTS* a numeric value and how R *STORES* a numeric value internally.

Regards,

Marc Schwartz