对于字符向量是否有tidyr::extract等价物?

对于字符向量是否有tidyr::extract等价物?,r,R,我在遇到这个问题后一直在思考 库(tidyverse) 种子(42) df #> [[2]] #> [1] "0" "25" #> #> [[3]] #> [1] "0" "25" #> #> [[4]] #> [1] "0" "25" #> #> [[5]] #> [1] "0" "25" #> #> [[6]] #> [1] "0" "25" 您可以在base R中使用strcapture: strcapture("(\\d+),(\\d+)", x_c

我在遇到这个问题后一直在思考

库(tidyverse)
种子(42)
df
#> [[2]]
#> [1] "0"  "25"
#> 
#> [[3]]
#> [1] "0"  "25"
#> 
#> [[4]]
#> [1] "0"  "25"
#> 
#> [[5]]
#> [1] "0"  "25"
#> 
#> [[6]]
#> [1] "0"  "25"

您可以在base R中使用
strcapture

strcapture("(\\d+),(\\d+)", x_chr, 
           proto = list(start = numeric(), end = numeric()))

#    start end
#1       0  25
#2       0  25
#3       0  25
#4       0  25
#5       0  25
#6       0  25
#...
#...
您还可以使用
stringr::str\u match

stringr::str_match(x_chr, "(\\d+),(\\d+)")[, -1]

str_match
中,第1列返回完整的模式,而所有后续列都是捕获组。

您可以通过
str_extract_all()
执行此操作:
str_extract_all(x_chr,“\\d+”)
。这仅在本例中有效-我希望有一种方法可以实际拆分为定义的组。很好,我当然不知道:)你知道stringi或stringr的等价物吗?如果不是,请不要担心。您可以使用
str\u match
/
stri\u match
。更新了答案。
strcapture("(\\d+),(\\d+)", x_chr, 
           proto = list(start = numeric(), end = numeric()))

#    start end
#1       0  25
#2       0  25
#3       0  25
#4       0  25
#5       0  25
#6       0  25
#...
#...
stringr::str_match(x_chr, "(\\d+),(\\d+)")[, -1]