Search code examples
rsearchassign

Search through range of variables, identify diseases of interest and return earliest date of disease diagnosis (R)


I have a df in R containing multiple columns describing the icd10 diagnoses that a given individual has been assigned over the study period, with dates of these diagnoses also recorded in seperate variables:

df = data.frame(ID = c(1001, 1002, 1003, 1004, 1005),
                Disease_code_1 = c('I802', 'G200','I802','', 'H356'),
                Disease_code_2 = c('A071','','G20','','H250'),
                Disease_code_3 = c('H250', '','','',''),
                Date_of_diagnosis_1 = c('12/06/1997','13/06/1997','14/02/2003','','18/20/2005'),
                Date_of_diagnosis_2 = c('12/06/1998','','18/09/2001','','12/07/1993'),
                Date_of_diagnosis_3 = c('17/09/2010','','','',''))

    ID Disease_code_1 Disease_code_2 Disease_code_3 Date_of_disease_1 Date_of_disease_2 Date_of_disease_3
1 1001           I802           A071           H250        12/06/1997        12/06/1998        17/09/2010
2 1002           G200                                      13/06/1997                                    
3 1003           I802            G20                       14/02/2003        18/09/2001                  
4 1004                                                                                                   
5 1005           H356           H250                       18/20/2005        12/07/1993                  

I would like to search through the Disease_code_* variables and return a 1 if an individual has been assigned any of the disease codes of interest, as specified in codes_of_interest = c("H250", "H356"), in addition to the earliest date any of the codes of interest were recorded. Ideally, my df will look like:

df = data.frame(ID = c(1001, 1002, 1003, 1004, 1005),
                Disease_of_interest = c('1','0','0','0','1'),
                Date_of_disease_interest = c('17/09/2010','','','','12/07/1993'),
                Disease_code_1 = c('I802', 'G200','I802','', 'H356'),
                Disease_code_2 = c('A071','','G20','','H250'),
                Disease_code_3 = c('H250', '','','',''),
                Date_of_diagnosis_1 = c('12/06/1997','13/06/1997','14/02/2003','','18/20/2005'),
                Date_of_diagnosis_2 = c('12/06/1998','','18/09/2001','','12/07/1993'),
                Date_of_diagnosis_3 = c('17/09/2010','','','',''))

    ID Disease_of_interest Date_of_disease_interest Disease_code_1 Disease_code_2 Disease_code_3 Date_of_disease_1 Date_of_disease_2 Date_of_disease_3
1 1001                   1               17/09/2010           I802           A071           H250        12/06/1997        12/06/1998        17/09/2010
2 1002                   0                                    G200                                      13/06/1997                                    
3 1003                   0                                    I802            G20                       14/02/2003        18/09/2001                  
4 1004                   0                                                                                                                            
5 1005                   1               12/07/1993           H356           H250                       18/20/2005        12/07/1993       

The code that I currently use to identify the disease codes of interest is (although this is insensitive to date of diagnosis):

dfs$Disease_of_interest<- apply(df[, -1], 1, function(x) {
  if(any(x %in% codes_of_interest))) {
    return(1)
  } else {
    return(0)
  }
}) 

Any advice you could provide on this would be greatly appreciated!


Solution

  • You can use %in% in apply to get the position where you find codes_of_interest which are then used in mapply to get min of Dates. If a date is retuned codes_of_interest is found, NA if it is not found.

    i <- apply(df[,2:4], 1, "%in%", codes_of_interest)
    mapply(function(x, i) if(any(i)) min(x[i]) else NA, asplit(df[,5:7], 1), asplit(i, 2))
    #[1] "17/09/2010" NA           NA           NA           "12/07/1993"