Okay, I'm a beginner and have issues formulating my questions adequately, so please redirect me if I can find the answer somewhere else and forgive me :)
I'm working with a messy big data patient file (> 40 million rows). Each patient (id) has several rows. Each row (roughly) represents one consultation with a symptom/disease code (icpc). I want to reduce the dataset to all rows of patients with specific conditions (icpc) in one of their consultations/rows, but keep all rows with other conditions for these patients as well. I would also like to keep the column reg.date.
My raw data.frame (x) looks something like this (this is fabricated data, id is much longer in my dataset and I left out irrelevant columns which I like to drop):
id icpc reg.date
123 D95 19JUN2015
123 F85 15AUG2016
332 A01 16MAR2010
332 A04 20JAN2018
332 K20 20FEB2017
100 B10 01JUN2017
100 A04 11JAN2008
113 T08 18MAR2018
113 P28 19JAN2017
113 D95 16JAN2013
113 A01 01MAY2009
551 B12 03APR2011
551 D95 09MAY2015
Say I want to only to keep the patients with D95 and/or A01 in the dataset. I've managed subsetting:
subset(x, ICPC == c("D95", "A01"))
But this leaves me only with the consultations where D95 and A01 was registered:
id icpc reg.date
123 D95 19JUN2015
332 A01 16MAR2010
113 D95 16JAN2013
113 A01 01MAY2009
551 D95 09MAY2015
This is the data.frame I want:
id icpc reg.date
123 D95 19JUN2015
123 F85 15AUG2016
332 A01 16MAR2010
332 A04 20JAN2018
332 K20 20FEB2017
113 T08 18MAR2018
113 P28 19JAN2017
113 D95 16JAN2013
113 A01 01MAY2009
551 B12 03APR2011
551 D95 09MAY2015
Any help would be greatly appreciated. Thank you!