Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove non-numeric characters from strings using gsub in R?

Tags:

regex

r

gsub

I use the gsub function in R to remove unwanted characters in numbers. So I should remove from the strings every character that is not a number, ., and -. My problem is that the regular expression is not removing some non-numeric characters like d, +, and <.

Below are my regular expression, the gsub execution, and its output. How can I change the regular expression in order to achieve the desired output?

Current output:

gsub(pattern = '[^(-?(\\d*\\.)?\\d+)]', replacement = '', x = c('1.2<', '>4.5', '3+.2', '-1d0', '2aadddab2','1.3h'))
[1] "1.2<"  ">4.5"  "3+.2"  "-1d0"  "2ddd2" "1.3"

Desired output:

[1] "1.2"  "4.5"  "3.2"  "-10"  "22" "1.3"

Thank you.

like image 323
jair.jr Avatar asked Oct 09 '18 13:10

jair.jr


People also ask

How do I get rid of non numeric characters in R?

We will remove non-alphanumeric characters by using str_replace_all() method. [^[:alnum:]] is the parameter that removes the non-alphanumeric characters.

How do I remove non numeric characters from a string?

In order to remove all non-numeric characters from a string, replace() function is used. replace() Function: This function searches a string for a specific value, or a RegExp, and returns a new string where the replacement is done.

How do I remove unwanted characters from R?

How to remove a character or multiple characters from a string in R? You can either use R base function gsub() or use str_replace() from stringr package to remove characters from a string or text.

How do I remove digits from a string in R?

To remove dot and number at the end of the string, we can use gsub function. It will search for the pattern of dot and number at the end of the string in the vector then removal of the pattern can be done by using double quotes without space.


1 Answers

Simply use

gsub("[^0-9.-]", "", x)

You can in case of multiple - and . have a second regEx dealing with that. If you struggle with it, open a new question.


(Make sure to change . with , if needed)

like image 76
Andre Elrico Avatar answered Sep 27 '22 17:09

Andre Elrico