is there any way to extract all numbers in a string as a vector? I have a large dataset which doesn't follow any specific pattern, so using the extract
+ regex
pattern won't necessarily extract all numbers. So for example for each row of data frame shown below:
c("3.2% 1ST $100000 AND 1.1% BALANCE", "3.3% 1ST $100000 AND 1.2% BALANCE AND $3000 BONUS FULL PRICE ONLY",
"$4000", "3.3% 1ST $100000 AND 1.2% BALANCE", "3.3% 1ST $100000 AND 1.2% BALANCE",
"3.2 - $100000")
[1] "3.2% 1ST $100000 AND 1.1% BALANCE"
[2] "3.3% 1ST $100000 AND 1.2% BALANCE AND $3000 BONUS FULL PRICE ONLY"
[3] "$4000"
[4] "3.3% 1ST $100000 AND 1.2% BALANCE"
[5] "3.3% 1ST $100000 AND 1.2% BALANCE"
[6] "3.2 - $100000"
I want to have an output like:
[1] "3.2 100000 1.1"
[2] "3.3 100000 1.2 3000"
[3] "4000"
[4] "3.3 100000 1.2 "
[5] "3.3 100000 1.2 "
[6] "3.2 100000 "
I had a look at resources and found this link:https://statisticsglobe.com/extract-numbers-from-character-string-vector-in-r
regmatches(x, gregexpr("[[:digit:]]+", x))
it seems that the above function works but it's not capable of doing this task on all sorts of numbers at once. I understand that "[[:digit:]]+"
only look for integer numbers but how we can change this so that it covers all sorts of numbers?