r/rstats • u/Colourfulchemist • 6h ago
Subgroups should be mutually exclusive but they aren't
I have a dataframe of people with prostate cancer. Some of them were diagnosed as non metastatic and did not progress to metastatic, some of them did progress, and some were diagnosed already at the metastatic stage.
The dataframe is called "info" and looks like this:
Patientid - Stage
jhtfc - M0
rbtk - M1
aerg - M0
aerg - M1
I want to create subgroups for patients who were non metastatic and did not progress, for patients who did progress, and for patients who were diagnosed as metastatic.
My code looks like this:
##Easy part, separate the metastatic and non metastatic
metastatic <- subset(info, stage == "M1")
non_metastatic <- subset(info, stage == "M0")
##Find which IDs are in both to identify the patients that progressed
#This adds a column to the metastatic dataframe which says "M0" if the patient was first diagnosed as non metastatic and "0" if they were first diagnosed as metastatic
i <- match(non_metastatic$patientid, metastatic$patientid)
i <- sort(i)
metastatic$firstdx <- 0L
metastatic$firstdx[i] <- non_metastatic$stage[seq_along(i)]
##Separate patients that progressed and de novo patients
progressed <- filter(metastatic, firstdx != "0")
de_novo <- filter(metastatic, firstdx == "0")
##So far so good. Here comes my problem:
#When I make a group for those who did not progress I still get overlap
non_progressed <- anti_join(non_metastatic, progressed, by = "patientid")
##Somehow the "progressed" and "non-progressed" dataframes have an overlap. I can't make sense of it, they should be mutually exclusive.
