Site icon R-bloggers

ICD code – search looping

[This article was first published on Matt's Stats n stuff » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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:

system.time(
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]])))
	print(paste(i,"of",length(icd_codes),"complete"))
	})
row.names(hosp_count)[1] <- "Counts"

Things to note:

Counting Unique Hospitilisations:
Here you can use like the following:
system.time(
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))
	print(paste(i,“of”,length(icd_codes),“complete”))
	})
row.names(hosp_count)[2] <- “Hosp count”

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).

To leave a comment for the author, please follow the link and comment on their blog: Matt's Stats n stuff » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.