Related questions here R/dplyr: How to only keep integers in a data frame? and here R extract first number from string but this attempts to capture a relatively common case in some of the social sciences.
Imagine data as in the example below that are a mixture of text and numerals in one field. Perhaps the result of some survey process. You want to extract all the numbers reliably and ignore the text but if there are multiple numbers, perhaps a range of values you want to take the mean
or median
or some other numeric function to give you one "best" answer.
library(dplyr)
library(stringr)
library(purrr)
Years <- c("0 to 1 year",
"5 yrs",
"About 10 yrs",
"20 yrs",
"4 yrs",
"4-5 yrs",
"75 to 100 YEARS old",
">1 yearsmispelled or whatever")
df <- data.frame(Years)