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.
Even though it uses
data.table
internally, the function is designed to return adata.frame
:It also whacks the original order:
If you can live with the order change, then -- since you are stuck loading
data.table
anyway -- just use its idiom for it: