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. Expe
I think that substitution is an indirect way of getting to the solution. If you want to retrieve all the numbers, I recommend gregexpr:
matches <- regmatches(years, gregexpr("[[:digit:]]+", years))
as.numeric(unlist(matches))
If you have multiple matches in a string, this will get all of them. If you're only interested in the first match, use regexpr instead of gregexpr and you can skip the unlist.