我有以下数据
data <- data.frame(make=c("af455f5","255tfd4d3","ojt100","12unin234mimo24","sh469dh","6ht6k778k9","kjh457"),
type=c("a","a","a","a","b","a","b"))
如果相应的类型是“a”,我只想从“make”元素中提取任何三位数字。
我尝试过使用gsub,但似乎只能提取组合字符串中的所有数字。
我正在寻找的结果如下
result <- c("455","255","100","234","778")
答案 0 :(得分:2)
这看起来像你想要的那样。
library(stringr)
str_extract(data$make,"[0-9]{3}")[data$type=="a"]
结果:
[1] "455" "255" "100" "234" "778"
希望这有帮助!
答案 1 :(得分:1)
这是我的答案:
library(stringr)
str_extract(data$make[data$type == 'a'], '[0-9]{3}')
## "455" "255" "100" "234" "778"
答案 2 :(得分: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"
答案 3 :(得分:0)
It can also be achieved using the strapplyc
from gsubfn
package:
result <- sapply(as.character(df$make[df$type=='a']),
function(x) strapplyc(x, "\\d{3}", simplify = TRUE))
output:
result
# af455f5 255tfd4d3 ojt100 12unin234mimo24 6ht6k778k9
# "455" "255" "100" "234" "778"