0

I have a dataset with ~ 4000 patients and a primary diagnosis given by ICD10 codes. Many of the diagnoses are repeated across patients. I found the "icd" R package.

I'd like it to add a column describing (in words) what the diagnosis is. Note that 2 patients have J12.81.

library(icd)
library(tidyverse)
library(tribble)

mydata <- tribble(
  ~ID, ~ICD10,
  1, "J12.81",
  2, "J44.9",
  3, "J12.81",
  4, "E84.0"
)

desired_output <-tribble(
  ~ID, ~ICD10, ~Description,
  1, "J12.81","Pneumonia due to SARS-associated coronavirus",
  2, "J44.9","Chronic obstructive pulmonary disease, unspecified",
  3, "J12.81","Pneumonia due to SARS-associated coronavirus",
  4, "E84.0","Cystic fibrosis with pulmonary manifestations"
)

I attempted it using this code

desired_output<-mydata %>%
  mutate(description = icd::explain_code(ICD10))

But I get an error description must be size 4 or 1, not 3. Which seems to suggest the explain_code is just getting a list of unique values?

Alternately, is there a way to get an explain_code that outputs the code and description together so I could do a left_join?

John Ryan
  • 343
  • 1
  • 9
  • 1
    Can you use `mydata %>% reframe(description = icd::explain_code(ICD10))` – akrun Feb 24 '23 at 17:51
  • i tried desired_output2 <- mydata %>% reframe(description = icd::explain_code(ICD10)) but that deleted the columns I have and just returned the 3 unique ICD codes - not 4 rows. – John Ryan Feb 24 '23 at 17:57
  • 1
    That package is not in CRAN, so it is difficult to test. Or you could wrap it in a list i.e. `mydata %>% rowwise %>% mutate(description = list(icd::explain_code(ICD10))) %>% ungroup() %>% unnest(description)` – akrun Feb 24 '23 at 17:58
  • WIth that code, you could also do a left join `mydata %>% reframe(description = icd::explain_code(ICD10)) %>% left_join(mydata, .)` – akrun Feb 24 '23 at 18:07

1 Answers1

0

It's not the explain_code function - it's the explain_table function that will work.

desired_output<-mydata %>%
  mutate(desc = icd::explain_table(ICD10)) %>%
  mutate(description = desc$short_desc) %>%
  select(-desc)
John Ryan
  • 343
  • 1
  • 9