I am trying to score each patient in my data based using their ICD10 codes.
library(dplyr)
library(comorbidity)
set.seed(1)
x <- data.frame(
pat_id = sample(100:999, size = 300, replace = TRUE),
code = sample_diag(n = 300)
)
I can successfully create the Charlson scores and the output contains a column with the original patient ids pat_id
charlson <- comorbidity(x = x, id = "pat_id", code = "code", map = "charlson_icd10_quan", assign0 = TRUE, tidy.codes = TRUE)
I've created a row number ID so that I can join to the scores output
charlson_ids <- charlson %>%
mutate(id = row_number()) %>%
select(id,pat_id)
When I convert the charlson index to scores there is no patient id so I am assuming row 1 in the output from charlson above ties to row 1 in scores below??
scores <- score(charlson, weights = NULL, assign0 = FALSE)
scores_df <- data.frame(scores) %>%
mutate(id = row_number())
combined <- charlson_ids %>%
inner_join(scores_df, by = c("id"="id")) %>%
select(-id)
If anyone can suggest a more succinct way to get from individual ICD-10 codes per patient to a comorbidity score per patient I would be grateful for any feedback.
the
score()
function returns a single value per row ofcharlson
(the output of thecomorbidity()
function). Therefore, you can simplify the above as:Created on 2022-03-15 by the reprex package (v2.0.1)
Alternatively, you can pipe all the way through:
...but that's just a matter of style, as you can see the results are the same.
Alessandro