在我的数据中,我有一列开放文本字段数据,类似于以下示例:
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",
)
我想使用
正则表达式
从中提取所有两位数字,放入一个名为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
但是,有没有一种方法可以将两个两位数提取到同一列并使用某些标准分隔符(例如逗号)?