从r中的角色向量提取有效数字
假设我有以下字符vector
c(“ hi”,“ 4”,“ -21”,“ 6.5”,“ 7.5”,“ -2.2”,“ 4H”)
现在i想要仅提取上述向量中的有效数字:
c(“ 4”,“ -21”,“ 6.5”,“ -2.2”)
注意:两者之间的一个空间。和 7中的5。 5
因此不是有效的数字。
我正在尝试使用Regex /^ - ?(0 | [1-9] \\ d*)(\\。\\ d+)?$/
, here ,但没有运气。
那么,从字符向量提取有效数字的正则是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们可以使用
grep
将数字与。字符串
或附带案例
We can use
grep
that matches digits with.
from the start (^
) till the end ($
) of the stringOr for fringe cases
as.numeric
已经做得很好。任何有效数字的东西都可以成功地胁迫到数字,其他所有内容都是na
。as.numeric
already does a great job of this. Anything that's a valid number can be successfully coerced to numeric, everything else isNA
.