ICD code – search looping

Following on from my earlier post on creating a table of ICD codes in R, here is how I am currently counting these codes and storing the codes in a dataframe:

Firstly create a dataframe to store the results in:

hosp_count <- as.data.frame(matrix(ncol=length(icd_codes)))
names(hosp_count) <- names(icd_codes)

Counting Occurences:

Then start to loop through your dataset with something similar to the following:

for(i in 1:length(icd_codes))
	hosp_count[1,i] <- sum( apply( my.data[ ,c("hosp_code_1","hosp_code_2")],2,
	function(x) sum(sub("..*","",x) %in% icd_codes[[i]])))
row.names(hosp_count)[1] <- "Counts"

Things to note:

  • Generally you will have multiple multiple columns in your data with the ICD codes in them, so you can add as many ‘hosp_codes’ as you have.
  • Subsetting is easy, you can use substitute in “my.data[my.data$case==”Yes”, c(…” or equivalent so you are only counting within a subset of your subjects, here, within the cases for some outcome.
  • The ‘paste’ command can be taken out, this is only in there as if you have a large data set you it might take some time to complete the count for each of the ’causes/sub chapter’ groupings, so it is good to be able to see where you are up to.
  • The sub function within the apply statement. This strips the decimal place off each code recording so “F35.7”, “F35.06” and “F35.49” all become “F35” and are then able to be matched to our list with the “%in%” command.
Counting Unique Hospitilisations:
Here you can use like the following:
for(i in 1:length(icd_codes))
	hosp <- c( apply(my.data[ ,c("hosp_code_1","hosp_code_2")],2,
	2, function(x) which(sub("..*","",x) %in% icd_codes[[i]])) , recursive=T)
	hosp_count[2,i] <- length(unique(hosp))
row.names(hosp_count)[2] <- "Hosp count"

Created by Pretty R at inside-R.org

This has the same things to note as above.
What is the difference between occurrences and unique hospitilisations?
Well, with having multiple codes per hospitilisation, if you are counting intestinal infectious disease codes (A00-A09), then you may have A01 for the first code and A04.4 as a 3rd or 4th diagnosis code. This will result in a count of 2 for that one single hospital visit. This may or may not be what you are after, you have to think carefully about the question you are asking. For most implementations I think that would only want to be counted once for each row (hospitilisation).

ICD codes – Analysing hospitilisations

A brief first post on what I hope will be a series of posts on analysing hospitilisation data, which is recorded using ICD codes (International Statistical Classification of Diseases and Related Health Problems)

Initially here is an R file. This can be read in and will create a list, 218 long, forming groupings using sub chapters of the ICD coding systems. With this you can then loop through your file of hospitilisations doing string searches (which sadly isn’t R’s strength really), and get counts of how many times your subjects are hospitilised for causes.


I’m still working on my string searching code but it is coming along nicely, so I will post this shortly and hopefully have some rough times for how long they take to run on different datesets. Currently I’m working with two files, 1.7 mil and 1.3 mil rows, with up to 21 diagnosis codes per record (row).

Below is a link to a file is a ‘dictionary’ for the abbreviated names used in the function above.