从字符串中提取前N个数字

时间:2018-07-18 12:29:41

标签: r

我只想从一些字符串中提取出前两个数字。 假设数据是:

ABC Conference Room Monitor - Z5580J    
ABC 19 Monitor    
ABC 24 Monitor for Video-Conferencing
ABC UltraSharp 24 Monitor -QU2482Z

所需的输出:

55
19
24
24

4 个答案:

答案 0 :(得分:1)

使用正则表达式\\D来匹配非数字字符,使用\\d{2}来匹配前两位数字的解决方案。

as.numeric(sub("\\D*(\\d{2}).*", "\\1", INPUT))
# [1] 55 19 24 24

数据:

INPUT <- c("ABC Conference Room Monitor - Z5580J",
           "ABC 19 Monitor",
           "ABC 24 Monitor for Video-Conferencing",
           "ABC UltraSharp 24 Monitor -QU2482Z")

答案 1 :(得分:1)

另一种解决方案:

strings <- c('ABC Conference Room Monitor - Z5580J','ABC 19 Monitor','ABC 24 Monitor for Video-Conferencing','ABC UltraSharp 24 Monitor -QU2482Z')
x <- as.numeric(gsub("\\D", "", strings))
as.numeric(substring(as.character(x*100), 1, 2))

[1] 55 19 24 24

答案 2 :(得分:0)

使用stringr的一种解决方案是:

library(stringr)
string <- str_extract_all("ABC Conference Room Monitor - Z5580J","\\(?[0-9,.]+\\)?")[[1]]
# "\\(?[0-9,.]+\\)?" is the regex, extracts only numbers
as.numeric(substr(string , 1,2)) # this selects the first two elements
#as.numeric is optional

答案 3 :(得分:0)

包装stringr可能允许使用最干净的解决方案:

stringr::str_extract(string, "\\d{2}")
 "55" "19" "24" "24"