如何使用 stringr::str_remove_all 删除字符串?

问题描述 投票:0回答:1

这是我的数据:

X <- c("9 % vol.", "6.5 % vol.", "8.5 % vol.", "10 % vol.", "7.5 % vol.")

我想把 X 变成一个数字向量,如下所示:

9, 6.5, 8.5, 10, 7.5

我尝试了以下代码:

stringr::str_remove_all(x, "[ //%//vol.]")

但是,这段代码给了我以下结果:

[1] "9"  "65" "85" "10" "75"

如何修改这段代码以获得我想要的输出?谢谢。

regex stringr
1个回答
1
投票

将您的代码修改为:

stringr::str_remove_all(X, "( % vol.)")

输出为:

"9"   "6.5" "8.5" "10"  "7.5"
© www.soinside.com 2019 - 2024. All rights reserved.