【问题标题】:how to read numbers from text如何从文本中读取数字
【发布时间】:2017-09-18 12:27:49
【问题描述】:
input                                                output
 car number and model     400708   

                              how to read just numeric form    

输入输出 车号和型号 400708

                              how to read just numeric form     from the above data                              

输入输出 车号和型号 400708

                              how to read just numeric form     from the above data                                                                                                                                                                                                                                            

【问题讨论】:

  • 这似乎是一个非常广泛的问题。请考虑先自己提出一个功能。 Stackoverflow 不是用来解决你的作业的。

标签: r


【解决方案1】:

我们可以使用sub提取

df1$output <- as.numeric(sub(".*\\s+", "", df1$input))

或者如果这是基于位置,使用子字符串

substring(df1$input, nchar(df1$input)-5)

【讨论】:

  • 感谢 akrun 对您使用的代码的解释,对我帮助很大。
  • 抱歉错误..只是低带宽让我点击了两次
  • 你能帮我解决这些问题吗?自从过去 4 天以来我一直在努力寻找正确的解决方案stackoverflow.com/q/46359191/8626662
【解决方案2】:

如果您想确保始终提取该 6 位代码,您可以使用一种方法来自动执行此操作,即使用正则表达式来执行此操作。

require(stringr)
input <- "S/O kk kumar sharma ropar rajasthan India 400708"
x<- str_extract(input, "\\d{6}$")

"" 告诉正则表达式您正在对字符串进行操作 \\d 说只取数字
[6} 说只取六位数
$ 说从字符串后面取它们

如果您的地址总是以这种方式出现,这是最有效的实施方式。

【讨论】:

  • 非常感谢我一直在为正则表达式而苦苦挣扎,因为很长时间以来也感谢您清除了这个概念
猜你喜欢
  • 2013-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
相关资源
最近更新 更多