[R] Searching within a ch. string
Wacek Kusnierczyk
Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Thu May 14 09:29:44 CEST 2009
RON70 wrote:
> Thanks for these suggestions. However I have one more question. Is there any
> way to extract only numbers? For example I want to extract only "88" in my
> example.
>
if you have just one integer number represented in a single string,
here's one way go:
strings = c('foo 1', '2 bar', 'and 3 as well')
(numbers = gsub('.*([0-9]+).*', '\\1', strings))
you will need to modify the regex if other sorts of numbers are encoded.
if more than one number is to be extracted from a single string, you can
try the package gsubfn:
strings = c('foo 1', '2 bar', 'and 3 and 4, too')
strapply(strings, '[0-9]+')
vQ
More information about the R-help
mailing list