Skip to content
Prev 361906 / 398506 Next

Extract character strings from a vector

You could remove all non-digits from the strings with
   > gsub("[^[:digit:]]+", "", x)
   [1] "0122"  ""      ""      "89963" "1"     "8"
and then count the number of characters remaining with nchar
  > x[nchar(gsub("[^[:digit:]]+", "", x)) <= 1]
  [1] "RTGFFFF" "GF TYHH" "KFTR1"   "RT 8"

Or you could do it with grep and a fancier regular expression
  > grep(value=TRUE, "^[^[:digit:]]*([[:digit:]][^[:digit:]]*){0,1}$", x)
  [1] "RTGFFFF" "GF TYHH" "KFTR1"   "RT 8"



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Tue, Jun 21, 2016 at 2:55 PM, Marine Regis <marine.regis at hotmail.fr>
wrote: