vendredi 23 août 2019

How to conditionally code a new variable based on checking multiple columns for multiple values

I want to search a series of values (ICD9 code list or ICD10 code list) within a group of variables (DX1-DX5) conditional of DXVER=9 search the ICD9 list and DXVER=10 search the ICD10 list. This is possible to write out by hand but because the variables and code lists can be longer I want to learn to do this by referring to the groups by a name.

I have tried if else statement, rowsum, and coding each individual step. These can work although I'm always missing one step out of the three requirements. 1. conditional on DXVER 2. refer to DXS for the list of DX1-DX5 3. refer to ICD9 and ICD10 for the code list

ICD9 <- c('042', '7953', '79571', 'V08')
ICD10 <- c('888', 'a10', 'b10', '987')

DXS <- c(paste('DX', seq(1:5), sep = '')) 

MergedData<-data.frame(DXVER=c("9", "9", "0", "0", "9"),
                   DX1 = c('042',"1","1","2","1"),
                   DX2 = c("4","3",'2',"4","3"),
                   DX3 = c("2","2","4","2","2"),
                   DX4 = c("3",'101',"2","3","2"),
                   DX5 = c('79571',"3",'888',"2","2"),
                   ID  = c(345, 123, 246, 432, 121))

MergedData$HIV_claim<NA
MergedData$HIV_claim[MergedData$DX1 %in% ICD9 & MergedData$DXVER=="9"] <- 
1 
MergedData$HIV_claim[MergedData$DX2 %in% ICD9 & MergedData$DXVER=="9"] <- 
1 
MergedData$HIV_claim[MergedData$DX3 %in% ICD9 & MergedData$DXVER=="9"] <- 
1 
MergedData$HIV_claim[MergedData$DX4 %in% ICD9 & MergedData$DXVER=="9"] <- 
1 
MergedData$HIV_claim[MergedData$DX5 %in% ICD9 & MergedData$DXVER=="9"] <- 
1 

MergedData$HIV_claim[MergedData$DX1 %in% ICD10 & MergedData$DXVER=="0"] <- 
1 
MergedData$HIV_claim[MergedData$DX2 %in% ICD10 & MergedData$DXVER=="0"] <- 
1 
MergedData$HIV_claim[MergedData$DX3 %in% ICD10 & MergedData$DXVER=="0"] <- 
1    
MergedData$HIV_claim[MergedData$DX4 %in% ICD10 & MergedData$DXVER=="0"] <- 
1 
MergedData$HIV_claim[MergedData$DX5 %in% ICD10 & MergedData$DXVER=="0"] <- 
1 


 MergedData2 <- MergedData

 MergedData2$HIV_claim9<NA
MergedData2$HIV_claim10<NA
MergedData2$HIV_claim9 <-  ((rowSums(MergedData2[, DXS] == ICD9,  na.rm=T) 
> 0) * 1)
MergedData2$HIV_claim10 <- ((rowSums(MergedData2[, DXS] == ICD10, na.rm=T) 
> 0) * 1)

MergedData2$HIV_claim  <-  ifelse(MergedData2$DXVER == 9, 
ifelse(MergedData2$DX1 %in% ICD9 |  MergedData2$DX2 %in% ICD9
 |  MergedData2$DX3 %in% ICD9 |  MergedData2$DX4 %in% ICD9
|  MergedData2$DX5 %in% ICD9,1,0),0)

Aucun commentaire:

Enregistrer un commentaire