I have a dataframe in R with multiple columns for different diagnoses and dates associated with each diagnosis.
df = data.frame(ID = c(1001, 1002, 1003, 1004, 1005),
Disease_code_1 = c('I802', 'G200','I802',NA, 'H356'),
Disease_code_2 = c('A071',NA,'G20',NA,'H250'),
Disease_code_3 = c('H250', NA,NA,NA,NA),
Date_of_diagnosis_1 = c('12/06/1997','13/06/1997','14/02/2003',NA,'18/20/2005'),
Date_of_diagnosis_2 = c('12/06/1998',NA,'18/09/2001',NA,'12/07/1993'),
Date_of_diagnosis_3 = c('17/09/2010',NA,NA,NA,NA))
ID Disease_code_1 Disease_code_2 Disease_code_3 Date_of_diagnosis_1 Date_of_diagnosis_2 Date_of_diagnosis_3
1 1001 I802 A071 H250 12/06/1997 12/06/1998 17/09/2010
2 1002 G200 <NA> <NA> 13/06/1997 <NA> <NA>
3 1003 I802 G20 <NA> 14/02/2003 18/09/2001 <NA>
4 1004 <NA> <NA> <NA> <NA> <NA> <NA>
5 1005 H356 H250 <NA> 18/20/2005 12/07/1993 <NA>
But the dates and first diagnoses are not in order across the variables. I have created a variable to list the date of first diagnosis as it is necessary for my covariates, but also need to know which diagnosis was first. I have tried an ifelse function, but am not sure how to link these together to create a "firstdiagnosis" variable.
df$mindate <- apply(data[,c("Disease_code_1","Disease_code_2","Disease_code_3")],1,min, na.rm = TRUE)