I have a string of numbers and characters
c2 = "list of 2nd C2 H2O 1 12 123"
I need to get rid of all digits that are actual numbers, i.e. 1, 12, 123, but not the ones that are part of a character set, i.e. 2nd, C2, H2O.
So far, the best solution I have come up with is this
gsub("? [[:digit:]]*", " ", c2)
"list of nd C2 H2O "
It successfully gets rid of 1 12 123, while retaining C2 H2O. However, I lost 2 in 2nd.
I am at my wits end.
Thanks!
Use the substr () Function to Remove the Last Characters in R Use the str_sub () Function to Remove the Last Characters in R Use the gsub () Function to Remove the Last Characters in R A string is an essential and common part of any programming language.
For the most easily readable code, you want the str_replace_all from the stringr package, though gsub from base R works just as well. The exact regular expression depends upon what you are trying to do. You could just remove those specific characters that you gave in the question, but it's much easier to remove all punctuation characters.
The gsub () function in R replaces or extracts strings by matching a specific pattern. To remove characters from the end using the gsub () function, we need to use regular expressions. See the following example.
We can also pass a string vector or a column name to the substr () function. The code below will show how we can remove the last two characters from a string vector: The str_sub () function is provided in the stringr package in R.
Try this:
> gsub("\\b\\d+\\b", "", c2)
[1] "list of 2nd C2 H2O "
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With