rfunctionforeachalphanumericicd

How do I expand out an alphanumeric string in R?


I have this alphanumeric character string of ICD-10 medical codes that come in ranges; however, I need to expand these out such that my final output is a string that reflects each medical code in each range. I've been at this for several days and chatgpt has not been helpful.

The prefix before the decimal change, as well as the endings change but follow a pattern (e.g., F11 through F19 for the prefix, and the endings have three ranges 120-129, 220-229, 920-929). I need all of these codes expanded out in a list or a data frame.

Here's just a sample of the alhpanumeric ranges I need to expand out:

test <- "F11.120-F11.129, F11.220-F11.229, F11.920-F11.929, F12.120-F12.129, F12.220-F12.229, F12.920-F12.929, F13.120-F11.129, F13.220-F11.229, F13.920-F11.929" 

I need the final output to look like this: "F11.120, F11.121, F11.122, F11.123, F11.124, F11.125, F11.126, F11.127, F11.128, F11.129, F11.220, ..., F19.929"

Would much apprecaite the help or an idea for how to approach the problem. Thank you!

This where my thought process is headed at this point after getting lost in trying to make a loop:

test <- strsplit(test, ",")[[1]]
codes <- str_split(test, "-", simplify = T)

nums <- codes %>% 
  as.data.frame() %>% 
  mutate(prefix = as.character(sub("\\..*", '',.[,1])),
         start_num = as.numeric(sub('.*\\.', '', .[,1])), 
         end_num = as.numeric(sub('.*\\.', '', .[,2]))
         )

Solution

  • Here's a helper function that parses the code using regular expressiosn and then expands the values with paste()

    expand_codes <- function(x) {
      parts <- strsplit(x, ",\\s+")[[1]]
      m <- regexec("F(\\d+)\\.(\\d+)-F(\\d+)\\.(\\d+)", parts)
      unlist(lapply(regmatches(parts, m), function(x) paste0("F", x[2], ".", as.numeric(x[3]):as.numeric(x[5]))))
    }
    

    It works like this

    test <- "F11.120-F11.129, F11.220-F11.229, F11.920-F11.929, F12.120-F12.129, F12.220-F12.229, F12.920-F12.929, F13.120-F11.129, F13.220-F11.229, F13.920-F11.929" 
    expand_codes(test)
    #  [1] "F11.120" "F11.121" "F11.122" "F11.123" "F11.124" "F11.125" "F11.126" "F11.127" "F11.128" "F11.129" "F11.220" "F11.221"
    # [13] "F11.222" "F11.223" "F11.224" "F11.225" "F11.226" "F11.227" "F11.228" "F11.229" "F11.920" "F11.921" "F11.922" "F11.923"
    # [25] "F11.924" "F11.925" "F11.926" "F11.927" "F11.928" "F11.929" "F12.120" "F12.121" "F12.122" "F12.123" "F12.124" "F12.125"
    # [37] "F12.126" "F12.127" "F12.128" "F12.129" "F12.220" "F12.221" "F12.222" "F12.223" "F12.224" "F12.225" "F12.226" "F12.227"
    # [49] "F12.228" "F12.229" "F12.920" "F12.921" "F12.922" "F12.923" "F12.924" "F12.925" "F12.926" "F12.927" "F12.928" "F12.929"
    # [61] "F13.120" "F13.121" "F13.122" "F13.123" "F13.124" "F13.125" "F13.126" "F13.127" "F13.128" "F13.129" "F13.220" "F13.221"
    # [73] "F13.222" "F13.223" "F13.224" "F13.225" "F13.226" "F13.227" "F13.228" "F13.229" "F13.920" "F13.921" "F13.922" "F13.923"
    # [85] "F13.924" "F13.925" "F13.926" "F13.927" "F13.928" "F13.929"