Search code examples
regexstringr

How to remove a string using stringr::str_remove_all?


Here is the data I have:

X <- c("9 % vol.", "6.5 % vol.", "8.5 % vol.", "10 % vol.", "7.5 % vol.")

I want to turn X into a numeric vector looking like:

9, 6.5, 8.5, 10, 7.5

I tried the following code:

stringr::str_remove_all(x, "[ //%//vol.]")

But, this code gives me the following outcome:

[1] "9"  "65" "85" "10" "75"

How can I modify this code so that I get the output I want? Thanks.


Solution

  • modify your code to:

    stringr::str_remove_all(X, "( % vol.)")
    

    output would be:

    "9"   "6.5" "8.5" "10"  "7.5"