Skip to content

Instantly share code, notes, and snippets.

@docsteveharris
Created November 27, 2014 12:13
Show Gist options
  • Save docsteveharris/ab5ad52bc5115d962cb2 to your computer and use it in GitHub Desktop.
Save docsteveharris/ab5ad52bc5115d962cb2 to your computer and use it in GitHub Desktop.
Create Charlson Score
score.charlson <- function(data) {
# data is a datatable containing the columns you need
# identify the columns with pmh in the name
require(data.table)
charlson.values <- data[,.(pmhmi, pmhhf, pmhpvd, pmhcvd, pmhdem, pmhcopd, pmhcopd, pmhctd, pmhud, pmhmld, pmhdm, pmhhemi, pmhckd, pmhdmend, pmhtumour, pmhleuk, pmhlymph, pmhsld, pmhmets, pmhaids)]
charlson.matrix <- as.matrix(charlson.values)
# Now I need to convert the NA's to 0 and >0 to 1 in matrix
charlson.matrix <- apply(charlson.matrix, 2, function(x) ifelse(is.na(x), 0, ifelse(x>=1,1, 0)))
# Now create a vector of scores
# Note that the order of the columns selected above *must* match the order of the scores here
charlson.weight <- matrix(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 6, 6))
# Matrix multiply where the 0 or 1 indicator now just creates a weight
r <- charlson.matrix %*% charlson.weight
# Now add column back (which asssumes the order is unchanged)
data <- data.table(data, r)
setnames(data, "V1", "charlson.score")
}
mydata <- score.charlson(data=mydata)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment