Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading