Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text String Isolation and Transformation in R

Tags:

regex

text

r

This df1 data frame looks really similar to something that I'm working with in real life (two columns):

df1 <- data.frame(provider = c("LeBron James, MD",
                          "Peyton Manning, DDS",
                          "Mike Trout, DO"),
             cpt_codes = c("This provider because he bills CPT codes 99284, 99282 and 99285 65% more than his peer group",
                           "Overutilization of visits per patient for E0781-RR-59 and J1100!",
                           "High units per patient compared to the specialty for the following:29581: 146.88% 93990: 33.71%"))

print(df1)
#             provider                                                                                       cpt_codes
#1    LeBron James, MD    This provider because he bills CPT codes 99284, 99282 and 99285 65% more than his peer group
#2 Peyton Manning, DDS                                Overutilization of visits per patient for E0781-RR-59 and J1100!
#3      Mike Trout, DO High units per patient compared to the specialty for the following:29581: 146.88% 93990: 33.71%

I need to extract all character blocks from the cpt_codes field that are 5 (alphanumeric) characters in length and end in a number (0:9). I then need to match them up to the provider field, containing a unique row for every provider/cpt_code combination. The end result looks like this:

#             provider cpt_codes
#1    LeBron James, MD     99284
#2    LeBron James, MD     99282
#3    LeBron James, MD     99285
#4 Peyton Manning, DDS     E0781
#5 Peyton Manning, DDS     J1100
#6      Mike Trout, DO     29581
#7      Mike Trout, DO     93990

Through research, I've found some really good stackoverflow questions and answers around text strings in R that have allowed me to piece-meal together my solution below. This solution gets me what I want, but it seems overly complicated. I'm looking forward to seeing if anyone else can come up with the 'final' output in a more concise manner.

library(stringr)
#replace all punctuation with spaces in the text strings
df1$cpt_codes <- str_replace_all(df1$cpt_codes, "[[:punct:]]", " ")

#identifies all 5 character blocks in the text strings
t <- str_extract_all(df1$cpt_codes, "\\b[a-zA-Z0-9]{5,5}\\b")

#makes a new data frame that keeps only the 5 character blocks ending in a numeric char
fn <- c(0:9)
cpts <- function(x) {
  t1 <- subset(t[[x]], grepl(paste(fn, collapse = "|"), substr(t[[x]], 5, 5)) == TRUE)
  data.frame(id = rep(x, length(t1)), cpt_codes = t1)
}
t2 <- do.call("rbind", (lapply(c(1:length(t)), function(x) cpts(x))))

#creates an "id" field on the df1
df1$id <- c(1:nrow(df1))
df3 <- df1[, -2]

final <- merge(df3, t2, by = "id")
final[, -1]

print(final)
#            provider cpt_codes
#1    LeBron James, MD     99284
#2    LeBron James, MD     99282
#3    LeBron James, MD     99285
#4 Peyton Manning, DDS     E0781
#5 Peyton Manning, DDS     J1100
#6      Mike Trout, DO     29581
#7      Mike Trout, DO     93990
like image 846
bshelt141 Avatar asked Mar 11 '23 17:03

bshelt141


1 Answers

You can try this regular expression \\b\\w{4}\\d\\b, besides I think [[:punct:]] is also a kind of word boundary so you don't have to replace them with white space.

library(dplyr); library(tidyr); library(stringr)
df1 %>% mutate(cpt_codes = str_extract_all(cpt_codes, "\\b\\w{4}\\d\\b")) %>% unnest()

#              provider cpt_codes
# 1    LeBron James, MD     99284
# 2    LeBron James, MD     99282
# 3    LeBron James, MD     99285
# 4 Peyton Manning, DDS     E0781
# 5 Peyton Manning, DDS     J1100
# 6      Mike Trout, DO     29581
# 7      Mike Trout, DO     93990
like image 71
Psidom Avatar answered Mar 20 '23 21:03

Psidom