In my data, I have a column of open text field data that resembles the following sample:
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",
)
I would like to use regex
to extract all two digit numbers to a new column called y
. I have the following code and it works well extracting the first match:
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
But, is there a way to extract both two-digit numbers to the same column with some standard separator (e.g comma)?