I need R code that will extract all the numbers in a string and then sum them.
Here is a sample string:
s <- c("123(11)56(10)89")
- Most strings will have at least nine numbers though in some cases the last value will be the letter 'x'.
- Double-digit numbers will be in parentheses.
- Not every string has a number in parentheses. Those that do could have more than one double-digit number in parentheses and their placement in a string can vary.
- The sample string contains nine numbers: 1,2,3,11,5,6,10,8,9.
- In the result string, I need to be able to access each number individually: e.g, what is the 5th number in the string? I also need to be able to sum all of a string's numbers, which for the sample string is 55.
In R, I tried to use the str_replace function, but was unable to get it to work.
I am familiar with 'dplyr'.
I reviewed stackoverflow posts that dealt with string issues, but could not find one that matched my situation. If I missed one that does, please let me know how I can view it.