I have seen several SO posts that seem to come close to answering this question but I cannot tell if any actually do so please forgive me is this is a duplicate post. I have several dozens of character strings (this a column within a data frame) that include different numbers, usually written out as words but sometimes as integers. E.g.:
Three neonates with one adult
1 adult, ten neonates nearby
Two adults and six neonates
My ultimate goal is to be able to extract the number of neonates and adults from each string and get something like this:
data.frame(Adults=c(1,1,6), Neonates=c(3,10,6)
But the number and location of the number within the string varies. All of the examples I have seen using gsub
, strsplit
, etc. seem to only work when the pattern used to substitute, split, extract, etc. is the same across strings or stays in a constant position within the string. Since I know that the numbers must be c("one","two",...,"ten")
, I could possibly loop through every character string and then loop through every possible number to see if it is present within the string and then, if present, extract it and convert to numeric. But this seems very inefficient.
Any help would be most appreciated!!