Converting icd9 codes in R, keep just the top result when there are multiples

648 views Asked by At

I am trying to convert icd9 codes to icd10 codes. Some of the conversions have multiple results. I just want to keep the top result and place it into a new column.

I have a dataframe named test

> test
   icd9
1  4260
2 41401
3 42821
4  8602
5  1869
6 41071

And the function convICD from the package icdcoder.

> test$icd10=convICD(test$icd9, "icd9")
Error in `$<-.data.frame`(`*tmp*`, icd10, value = list(icd9 = c("1869",  : 
  replacement has 7 rows, data has 6

It throws an error since some conversions have more than one result. For example code 1869 converts to two different values.

> convICD(1869, "icd9")
  icd9 icd10
1 1869 C6210
2 1869 C6290

I just want to create a new column in test, test$icd10 which has the first result from each version. So for example when it converts 1869 it will just convert it to C6210. I am sure there is a really simple solution for this, but I can't think of it.

1

There are 1 answers

3
hrbrmstr On
library(icdcoder)
library(data.table)

test <- data.frame(icd9 = c(4260, 41401, 42821, 8602, 1869, 41071))

Even though it uses data.table internally, the function is designed to return a data.frame:

str(convICD(test$icd9, "icd9"))
## 'data.frame':    7 obs. of  2 variables:
##  $ icd9 : chr  "1869" "1869" "41071" "41401" ...
##  $ icd10: chr  "C6210" "C6290" "I214" "I2510" ...
## 'data.frame':    7 obs. of  2 variables:
##  $ icd9 : chr  "1869" "1869" "41071" "41401" ...
##  $ icd10: chr  "C6210" "C6290" "I214" "I2510" ...

It also whacks the original order:

convICD(test$icd9, "icd9")
##    icd9   icd10
## 1  1869   C6210
## 2  1869   C6290
## 3 41071    I214
## 4 41401   I2510
## 5  4260    I442
## 6 42821   I5021
## 7  8602 S271XXA

If you can live with the order change, then -- since you are stuck loading data.table anyway -- just use its idiom for it:

res <- data.table(convICD(test$icd9, "icd9"))
data.frame(res[, .SD[1], by="icd9"])
##    icd9   icd10
## 1  1869   C6210
## 2 41071    I214
## 3 41401   I2510
## 4  4260    I442
## 5 42821   I5021
## 6  8602 S271XXA