如何在R中使用gsub从字符串中删除非数字字符? [英] How can I remove non-numeric characters from strings using gsub in R?
问题描述
我使用R中的gsub
函数删除数字中不需要的字符.因此,我应该从字符串中删除不是数字,.
和-
的每个字符.我的问题是正则表达式不能删除某些非数字字符,例如d
,+
和<
.
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 <
.
下面是我的正则表达式,gsub
执行及其输出.如何更改正则表达式以实现所需的输出?
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?
当前输出:
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"
所需的输出:
[1] "1.2" "4.5" "3.2" "-10" "22" "1.3"
谢谢.
推荐答案
只需使用
gsub("[^0-9.-]", "", x)
在多个-
和.
的情况下,您可以使用第二个regEx处理该问题.
如果您遇到困难,请打开一个新问题.
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)
这篇关于如何在R中使用gsub从字符串中删除非数字字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!