I have string like this:
years<-c("20 years old", "1 years old")
I would like to grep only the numeric number from this vector. Expected output is a vector:
c(20, 1)
How do I go about doing this?
I have string like this:
years<-c("20 years old", "1 years old")
I would like to grep only the numeric number from this vector. Expected output is a vector:
c(20, 1)
How do I go about doing this?
Here's an alternative to Arun's first solution, with a simpler Perl-like regular expression:
Extract numbers from any string at beginning position.
Extract numbers from any string INDEPENDENT of position.
How about
or
or
After the post from Gabor Grothendieck post at the r-help mailing list
Or simply:
You could get rid of all the letters too:
Likely this is less generalizable though.