如何在 R 中扩展字母数字字符串?

问题描述 投票:0回答:2
我有这个包含范围内的 ICD-10 医疗代码的字母数字字符串;但是,我需要扩展它们,以便我的最终输出是一个反映每个范围内的每个医疗代码的字符串。我已经这样做好几天了,chatgpt 没有帮助。

小数点之前的前缀发生变化,结尾也发生变化,但遵循一定的模式(例如,前缀为 F11 到 F19,结尾有 120-129、220-229、920-929 三个范围)。我需要将所有这些代码展开在列表或数据框中。

这只是我需要扩展的字母数字范围的示例:

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"
我需要最终输出如下所示:
“

F11.120、F11.121、F11.122、F11.123、F11.124、F11.125、F11.126、F11.127、F11.128、F11.129、F11.220、. ..,F19.929"

非常感谢您提供帮助或解决问题的想法。谢谢!

在试图制作一个循环时迷失之后,我的思维过程正在朝着这个方向发展:

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])) )
    
r function foreach alphanumeric icd
2个回答
1
投票
使用

gregexpr

> gregexpr('F\\d{2}\\.\\d{3}', test) |> + regmatches(x=test) |> + unlist() [1] "F11.120" "F11.129" "F11.220" "F11.229" "F11.920" "F11.929" [7] "F12.120" "F12.129" "F12.220" "F12.229" "F12.920" "F12.929" [13] "F13.120" "F11.129" "F13.220" "F11.229" "F13.920" "F11.929"
    

0
投票
这是一个辅助函数,它使用正则表达式解析代码,然后使用

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])))) }
它的工作原理是这样的

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"
    
© www.soinside.com 2019 - 2024. All rights reserved.