0

Brand new to R here. I'm currently trying to figure out how many patients have heart failure (HF) and diabetes (DM). I've figured out how many people have one or the other but now I need to figure out how to find the comorbidity. Is there a way to create a numerical value for for DM only (1), HFpEF only (2), and DM+HFpEF(3) and neither (0)

Here is the code I've done to find the incidence of the two individually. 0 1 and 2 refer to treatment groups. Since I already have the variables defined, can I add them together to get DM+HFpEF?

DM_HFpEF_together$hfpef_1 <- ifelse (DM_HFpEF_together$ea_2> 1.5, 1, 0)
table(DM_HFpEF_together$hfpef_1 [DM_HFpEF_together$grp == 0])
table(DM_HFpEF_together$hfpef_1 [DM_HFpEF_together$grp == 1])
table(DM_HFpEF_together$hfpef_1 [DM_HFpEF_together$grp == 2])

DM_HFpEF_together$dmstatus_1 <- ifelse (DM_HFpEF_together$dmstatus_1 == 'Y', 1, 0)
table(DM_HFpEF_together$dmstatus_1 [DM_HFpEF_together$grp == 0])
table(DM_HFpEF_together$dmstatus_1 [DM_HFpEF_together$grp == 1])
table(DM_HFpEF_together$dmstatus_1 [DM_HFpEF_together$grp == 2])
  • 1
    You might want look at the interaction function. – IRTFM Aug 04 '21 at 21:35
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Aug 04 '21 at 21:58

1 Answers1

0

You might want to try using the case_when() function from the dbplyr package along with the mutate() function from the dplyr package.

library(dbplyr) #for the case when function
library(dplyr) # for the mutate function

set.seed(25) #for reproducibility
pt<-paste("S", seq(1:10), sep="") #Fake patients
DM<-sample(c("Y", "N"), 10, replace=TRUE) #Random diabetes diagnoses
set.seed(68) #New seed to get a different random draw
HF<-sample(c("Y", "N"), 10, replace=TRUE) #Random Heart Failure diagnoses
DF<-data.frame(Patient=pt, Diabetes=DM, Heart_Failure=HF)#Create a dataframe of fake data
DF_Final<-DF %>% 
mutate(Comorbidity=case_when(Diabetes =="Y" & Heart_Failure=="N" ~ 1,
                                              Diabetes =="N" & Heart_Failure == "Y" ~ 2,
                                              Diabetes =="Y" & Heart_Failure=="Y" ~ 3,
                                              Diabetes =="N" & Heart_Failure=="N" ~ 0)) 

Here the use of the %>% or "pipe" send the dataframe into the first argument of the mutate() function. The mutate() function creates a new column in the dataframe. The case_when() function lets you conditionally reclassify a variable based on conditions of other variables. The syntax mutate(CoMorbidity = case_when(Diabetes=="Y" & Heart_Failure=="N" ~ 1)) means for rows where the value of diabetes is Y and the value of Heart_Failure is N, set the value of Comorbidity to 1.

Sean McKenzie
  • 707
  • 3
  • 13
  • [`case_when`](https://www.rdocumentation.org/packages/dplyr/versions/0.7.8/topics/case_when) is available in `dplyr` – Parfait Aug 05 '21 at 00:49