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

女生的网名这么多〃 提交于 2019-12-01 08:11:35

问题


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.


回答1:


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)



来源:https://stackoverflow.com/questions/52722846/how-can-i-remove-non-numeric-characters-from-strings-using-gsub-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!