首页 > 解决方案 > 如何使用正则表达式识别 icd10 数据中的模式

问题描述

我正在使用 icd10 数据,我希望使用正则表达式基于模式“E1X.9X”创建称为复杂性的新变量,但我不断收到错误消息。请帮忙

dm_2$icd9_9code<- (E10.49, E11.51, E13.52, E13.9, E10.9, E11.21, E16.0)

dm_2$DM.complications<- "present"
dm_2$DM.complications[regexpr("^E\\d{2}.9$",dm_2$icd9_code)]<- "None"

# Error in dm_2$DM.complications[regexpr("^E\\d{2}.9", dm_2$icd9_code)] <- 
# "None" : only 0's may be mixed with negative subscripts

我想

icd9_9code     complications
E10.49          present
E11.51          present
E13.52          present
E13.9           none
E10.9           none
E11.21          present

标签: r

解决方案


似乎您的代码中有一些错误,我会在下面的代码中记录它们:

您需要先用引号包裹您的 ICD 代码: "E13.9"

dm_2 <- data.frame(icd9_9code = c("E10.49", "E11.51", "E13.52", "E13.9", "E10.9", "E11.21", "E16.0"))

接下来让我们使用grepl()来搜索特定的 ICD 模式。确保您将其应用于正确的列,您上面的代码正在尝试使用dm_2$icd9_code而不是dm_2$icd9_9code

dm_2$DM.complications <- "present"
dm_2$DM.complications[grepl("^E\\d{2}.9$", dm_2$icd9_9code)] <- "None"

最后,

dm_2
#>   icd9_9code DM.complications
#> 1     E10.49          present
#> 2     E11.51          present
#> 3     E13.52          present
#> 4      E13.9             None
#> 5      E10.9             None
#> 6     E11.21          present
#> 7      E16.0          present

一个快速的旁注——还有一个很棒的 ICD 包,你可能会发现它很方便:https ://cran.r-project.org/web/packages/icd/index.html


推荐阅读