[R] regex find anything which is not a number

Steve Taylor steve.taylor at aut.ac.nz
Thu Mar 12 20:43:20 CET 2015

How about letting a standard function decide which are numbers:


Also works with numbers in scientific notation and (presumably) different decimal characters, e.g. comma if that's what the locale uses.

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Adrian Du?a
Sent: Thursday, 12 March 2015 8:27a
To: r-help at r-project.org
Subject: [R] regex find anything which is not a number

Hi everyone,

I need a regular expression to find those positions in a character
vector which contain something which is not a number (either positive
or negative, having decimals or not).

myvector <- c("a3", "N.A", "1.2", "-3", "3-2", "2.")

In this vector, only positions 3 and 4 are numbers, the rest should be captured.
So far I am able to detect anything which is not a number, excluding - and .

> grep("[^-0-9.]", myvector)
[1] 1 2

I still need to capture positions 5 and 6, which in human language
would mean to detect anything which contains a "-" or a "." anywhere
else except at the beginning of a number.

Thanks very much in advance,

Adrian Dusa
University of Bucharest
Romanian Social Data Archive
Soseaua Panduri nr.90
050663 Bucharest sector 5

R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list