I want to extract the stream code in R from the left column string. They are 1 to 2 digits long just after the last hyphen. Hope you can help. Thank you.
library(tibble)
service <- tibble('services' = c('HIPREHAB-HIPREHAB-1-F0752',
'HIPCCM-DIABCM-28-F0421',
'HIPSCTS-WOUND-6-F0652'
))
This code has three ways to extract the one or two digits between the hyphens.
library(stringr)
library(dplyr)
service <- tibble('services' = c('HIPREHAB-HIPREHAB-1-F0752',
'HIPCCM-DIABCM-28-F0421',
'HIPSCTS-WOUND-6-F0652'
))
service <- service |>
mutate(
#extract "hyphen digits hyphen" then extract the digits from that
stream = str_extract(str_extract(services, "-\\d{1,2}-"),"\\d+"),
#extract the first digits, won't work if the preceding text can have digits
stream2 = str_extract(services, "\\d{1,2}"),
#extract the digits from between hyphens
stream3 = str_extract(services, "(?<=-)\\d{1,2}(?=-)"))
service
#> # A tibble: 3 x 4
#> services stream stream2 stream3
#> <chr> <chr> <chr> <chr>
#> 1 HIPREHAB-HIPREHAB-1-F0752 1 1 1
#> 2 HIPCCM-DIABCM-28-F0421 28 28 28
#> 3 HIPSCTS-WOUND-6-F0652 6 6 6