【发布时间】:2020-04-27 11:58:46
【问题描述】:
在我的数据中,我有一列类似于以下示例的打开文本字段数据:
d <- tribble(
~x,
"i am 10 and she is 50",
"he is 32 and i am 22",
"he may be 70 and she may be 99",
)
我想使用regex 将所有两位数字提取到一个名为y 的新列中。我有以下代码,它可以很好地提取第一个匹配项:
d %>%
mutate(y = str_extract(x, "([0-9]{2})"))
# A tibble: 3 x 2
x y
<chr> <chr>
1 i am 10 and she is 50 10
2 he is 32 and i am 22 32
3 he may be 70 and she may be 99 70
但是,有没有办法使用一些标准分隔符(例如逗号)将两个两位数提取到同一列?
【问题讨论】:
-
这篇文章应该会有所帮助:stackoverflow.com/q/57059625/5325862。澄清一下,您只想提取两位数?