从R中的字符串中提取数字 [英] Extracting numbers from string in R

查看:2072
本文介绍了从R中的字符串中提取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含随机字符的字符串列表,如:

I have a list of strings which contain random characters such as:

list=list()
list[1] = "djud7+dg[a]hs667"
list[2] = "7fd*hac11(5)"
list[3] = "2tu,g7gka5"

我想知道哪些数字至少存在一次( unique())。我的示例的解决方案是:

I want to know which numbers are present at least once (unique()) in this list. The solution of my example is:

解决方案: c(7,667,11,5,2)

如果有人有一个方法不认为11是十一而是一一,它也将是有用的。在这种情况下的解决方案是:

If someone has a method that does not consider 11 as "eleven" but as "one and one", it would also be useful. The solution in this condition would be:

解决方案: c(7,6,1,5,2)

(我在相关主题中找到此帖子:从向量(字符串)中提取数字

(I found this post on a related subject: Extracting numbers from vectors (of strings))

推荐答案

使用 gsub 从字符串中删除不是数字的所有内容,然后拆分字符串,如下所示:

For the second answer, you can use gsub to remove everything from the string that's not a number, then split the string as follows:

unique(as.numeric(unlist(strsplit(gsub("[^0-9]", "", unlist(ll)), ""))))
# [1] 7 6 1 5 2

对于第一个答案,类似地使用 strsplit

For the first answer, similarly using strsplit,

unique(na.omit(as.numeric(unlist(strsplit(unlist(ll), "[^0-9]+")))))
# [1]   7 667  11   5   2


b $ b

PS:不要命名您的变量 list (因为有一个内置函数 list )。我将您的数据命名为 ll

PS: don't name your variable list (as there's an inbuilt function list). I've named your data as ll.

这篇关于从R中的字符串中提取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆