2017-08-04 79 views
2

提取3位數字,我有以下數據ř只有從字符串

data <- data.frame(make=c("af455f5","255tfd4d3","ojt100","12unin234mimo24","sh469dh","6ht6k778k9","kjh457"), 
       type=c("a","a","a","a","b","a","b")) 

我只希望從「製造」的元素中提取任何三位數如果相應的類型是「A」。

我試過使用gsub,但似乎只能提取組合字符串中的所有數字。

我在尋找的結果如下

result <- c("455","255","100","234","778") 

回答

2

這看上去好像是你想要做什麼。

library(stringr) 
str_extract(data$make,"[0-9]{3}")[data$type=="a"] 

結果:

[1] "455" "255" "100" "234" "778" 

希望這有助於!

1

這裏是我的答案:

library(stringr) 

str_extract(data$make[data$type == 'a'], '[0-9]{3}') 

## "455" "255" "100" "234" "778" 
1

我們可以使用base R方式完成本次

v1 <- data$make[data$type == "a"] 
regmatches(v1, regexpr("[0-9]{3}", v1)) 
#[1] "455" "255" "100" "234" "778" 

或用sub

sub("^.*([0-9]{3}).*", "\\1", v1) 
#[1] "455" "255" "100" "234" "778" 
0

它也可以用個實現Èstrapplycgsubfn包:

result <- sapply(as.character(df$make[df$type=='a']), 
      function(x) strapplyc(x, "\\d{3}", simplify = TRUE)) 

輸出:

result 
# af455f5  255tfd4d3   ojt100 12unin234mimo24  6ht6k778k9 
# "455"   "255"   "100"   "234"   "778"