Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove only "actual numbers" from a string of characters in R

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!

like image 602
wen Avatar asked Feb 08 '14 23:02

wen


People also ask

How to remove the last characters in R?

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.

How to remove all punctuation from a regular expression in R?

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.

How to remove characters from the end using GSUB () function in R?

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.

How to remove the last two characters from a string vector?

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.


Video Answer


1 Answers

Try this:

> gsub("\\b\\d+\\b", "", c2)
[1] "list of 2nd C2 H2O   "
like image 56
G. Grothendieck Avatar answered Sep 27 '22 22:09

G. Grothendieck