@@ -360,13 +360,13 @@ comorbidities.data.frame <- function(data,
360360 # Determine the lookup table and the columns for the lookup table to keep
361361 lookup_to_keep <- c(" condition" )
362362 if (startsWith(method , " pccc" )) {
363- lookup <- get_pccc_codes( )
363+ lookup <- get( x = " pccc_codes " , envir = ..mdcr_data_env.. , inherits = FALSE )
364364 lookup_to_keep <- c(lookup_to_keep , " subcondition" , " transplant_flag" , " tech_dep_flag" )
365365 } else if (startsWith(method , " charlson" )) {
366- lookup <- get_charlson_codes( )
366+ lookup <- get( " charlson_codes " , envir = ..mdcr_data_env.. , inherits = FALSE )
367367 lookup_to_keep <- c(lookup_to_keep )
368368 } else if (startsWith(method , " elixhauser" )) {
369- lookup <- get_elixhauser_codes( )
369+ lookup <- get( " elixhauser_codes " , envir = ..mdcr_data_env.. , inherits = FALSE )
370370 lookup_to_keep <- c(lookup_to_keep , " poaexempt" )
371371 }
372372
@@ -385,25 +385,21 @@ comorbidities.data.frame <- function(data,
385385 # #############################################################################
386386 # inner join the data with the lookup table
387387 on_full <-
388- merge (
388+ mdcr_inner_join (
389389 x = if (full.codes ) {data } else {data [0 , ]},
390390 y = lookup ,
391- all = FALSE ,
392391 by.x = by_x ,
393392 by.y = c(" full_code" , by_y ),
394- suffixes = c(" " , " .y" ),
395- sort = FALSE
393+ suffixes = c(" " , " .y" )
396394 )
397395
398396 on_comp <-
399- merge (
397+ mdcr_inner_join (
400398 x = if (compact.codes ) {data } else {data [0 , ]},
401399 y = lookup ,
402- all = FALSE ,
403400 by.x = by_x ,
404401 by.y = c(" code" , by_y ),
405- suffixes = c(" " , " .y" ),
406- sort = FALSE
402+ suffixes = c(" " , " .y" )
407403 )
408404
409405 # #############################################################################
@@ -519,6 +515,7 @@ comorbidities.data.frame <- function(data,
519515 grps <- c(grps , " subcondition" )
520516 byconditions <- c(byconditions , " subcondition" )
521517 }
518+ # identify first occurrence per id/condition then retain encounters on/after it
522519 tmp <- mdcr_select(cmrb , c(grps , encid ))
523520 tmp <- mdcr_setorder(tmp , c(grps , encid ))
524521 keep <- ! mdcr_duplicated(tmp , by = grps )
@@ -527,12 +524,11 @@ comorbidities.data.frame <- function(data,
527524
528525 # merge on the poa.var
529526 foc <-
530- merge(x = foc ,
531- y = cmrb ,
532- all = TRUE ,
533- by.x = c(id.vars2 , " first_occurrance" , byconditions ),
534- by.y = c(id.vars2 , encid , byconditions ),
535- sort = FALSE
527+ mdcr_full_outer_join(
528+ x = foc ,
529+ y = cmrb ,
530+ by.x = c(id.vars2 , " first_occurrance" , byconditions ),
531+ by.y = c(id.vars2 , encid , byconditions )
536532 )
537533
538534 if (startsWith(method , " pccc" )) {
@@ -546,7 +542,7 @@ comorbidities.data.frame <- function(data,
546542 foc <-
547543 lapply(foc ,
548544 function (y ) {
549- rtn <- merge (x = iddf , y = y , all.x = TRUE , by = c(id.vars2 ), allow.cartesian = TRUE , sort = FALSE )
545+ rtn <- mdcr_left_join (x = iddf , y = y , by = c(id.vars2 ))
550546 rtn <- mdcr_subset(rtn , i = ! is.na(rtn [[" condition" ]]))
551547 i <- rtn [[encid ]] > = rtn [[" first_occurrance" ]]
552548 mdcr_subset(rtn , i = i )
@@ -619,17 +615,6 @@ comorbidities.data.frame <- function(data,
619615
620616 # #############################################################################
621617 # set attributes and return
622- if (requireNamespace(" tibble" , quietly = TRUE ) && inherits(data , " tbl_df" )) {
623- if (subconditions ) {
624- ccc [[" conditions" ]] <- getExportedValue(name = " as_tibble" , ns = " tibble" )(x = ccc [[" conditions" ]])
625- for (i in seq_len(length(ccc [[" subconditions" ]]))) {
626- ccc [[" subconditions" ]][[i ]] <- getExportedValue(name = " as_tibble" , ns = " tibble" )(x = ccc [[" subconditions" ]][[i ]])
627- }
628- } else {
629- ccc <- getExportedValue(name = " as_tibble" , ns = " tibble" )(x = ccc )
630- }
631- }
632-
633618 attr(ccc , " method" ) <- method
634619 attr(ccc , " id.vars" ) <- id.vars
635620 attr(ccc , " flag.method" ) <- flag.method
0 commit comments