清洁以下字符串并将此字符向量C转换为整数类型的向量类型

发布于 2025-02-08 21:04:38 字数 321 浏览 0 评论 0原文

字符串:

c1 <- c("cSVgKl9yyb", "e7w4o11oh8", "iYdWYvV7b2", "Epal3cNuGH", "NNhbMR0ocT", "fYaRvoag8B", 
       "LO4fkHm7Kn", "JK8jKhS5De", "DcMAZ7Rxtp", "sV0tqC8XSd")

输出必须是:

[1]     9 74118    72     3     0     8    47    85     7     8

任何知道解决此问题的代码的人?

The string:

c1 <- c("cSVgKl9yyb", "e7w4o11oh8", "iYdWYvV7b2", "Epal3cNuGH", "NNhbMR0ocT", "fYaRvoag8B", 
       "LO4fkHm7Kn", "JK8jKhS5De", "DcMAZ7Rxtp", "sV0tqC8XSd")

Output must be:

[1]     9 74118    72     3     0     8    47    85     7     8

Anyone who knows the code to solve this question?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

影子的影子 2025-02-15 21:04:38
library(stringr)    
str_replace_all(c1, "[^0-9]", "")
[1] "9"     "74118" "72"    "3"     "0"     "8"     "47"    "85"    "7"     "08"

在这里,我们删除了数字类中包含的所有内容,其中“非”是通过^表示的,而数字类则由0-9表示。另外,正如@yonyambu所建议的那样,您可以将否定字符类用于不是的任何东西,\\ d

如果您希望值是数字:

as.numeric(str_replace_all(c1, "[^0-9]", ""))
[1]     9 74118    72     3     0     8    47    85     7     8
library(stringr)    
str_replace_all(c1, "[^0-9]", "")
[1] "9"     "74118" "72"    "3"     "0"     "8"     "47"    "85"    "7"     "08"

Here we remove everything that is not contained in the class of digits, where "not" is expressed via ^ and the class of digits is expressed by 0-9. Alternatively, as suggested by @onyambu, you can use the negative character class for anything that is not a digit, \\D.

If you want the values to be numeric:

as.numeric(str_replace_all(c1, "[^0-9]", ""))
[1]     9 74118    72     3     0     8    47    85     7     8
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文