Using the package unglue we can do :
# install.packages("unglue")
library(unglue)
years<-c("20 years old", "1 years old")
unglue_vec(years, "{x} years old", convert = TRUE)
#> [1] 20 1
Created on 2019-11-06 by the reprex package (v0.3.0)
More info: https://github.com/moodymudskipper/unglue/blob/master/README.md